Implementation

Compiler

The compiler for R1 language has been constructed and ported to two machines, PC and Sun Sparc. The compiler is written in C for portability. The previous version of this compiler was written in Pascal on a PC and was completely rewritten in C for porting to Unix machines. The size of source program of the compiler is approximately 1800 lines.

It is a two-pass compiler. The first pass processes all global declarations and produced an intermediate file “tmp000.bak”. The intermediate file which is used by the second pass, contains only the body of code. All declarations and comments are stripped out. All global names are known by the end of the first pass. The second pass reads the intermediate file, processes the body of code and generates i-code in the memory. At the end of successful compilation, the i-code is post-processed to improve its speed of execution. The buffer is written out with a header containing information necessary for an interpreter to check the validity of the executable code. The header is 16 bytes long and has the following format :

putInt(MAGIC);
putInt(VER);
putInt(csize);
putInt(dsize);
putInt(psize);
putInt(0);
putInt(0);
putInt(0);
where putInt writes 2 bytes binary, hi byte first, MAGIC = 0x1abc, VER = 1 for this release, csize, dsize and psize are the size of code, data and the number of process consecutively.

The source code of compiler contains the following parts :

lexical analyser 300 lines
parser 850 lines
code generator 150 lines
symbol table 250 lines
others (.h etc.) 200 lines
total approx. 1800 lines
The compiler on PC is compiled by Turbo C v 2.0 and it is tested to work properly under DOS, MS Windows 3.11, MS Windows 95. The version on Sun Solaris is compiled by GNU C compiler and is tested to work under Solaris 2.3. As this is a simple recursive descent compiler, the compilation speed is reasonably fast. The source code of compiler are arranged as following :

compile.c, dcl.c, expr.c, head.c, icode.c, lex.c, stmt.c, symtab.c

The compiler accepts input source file (in R1) and produces an I-code file (also produces a readable version of I-code). The command line

c> compile test.txt

produces test.out (I-code file) and test.lst (readable I-code).

Interpreter

The interpreter is written in C for portability (using old Kernigan and Ritchie’s style C). It is compiled and tested under Solaris 2.3 using GNU C compiler and on PC using Turbo C v2.0 under DOS and Windows. The source files are arrange as follows:
interpreter 330 lines
process 300 lines
message 230 lines
timer 150 lines
others 80 lines
total approx. 1000 lines
and consisted of the following files :

interp.c, message.c, process.c, timer.c

The interpreter is implemented entirely in high level language eventhough the real-time facilities required an access to low-level functions. The system time is a 32-bit counter. It is incremented through the interrupt service routine of the underlying operating system. On PC, we use the interrupt vector 0x1c which has the frequency 18.2 Hz. On Unix, we use signal() via setitimer(). The frequency of interruption can be adjusted in microsecond unit. However, the actual granuality of the resolution of alarm time on Unix is platform-dependent. If we assume the timer ticks at 20 Hz., the 32-bit system time in the interpreter will last approximately 60,000 hours or about 7 years. This should be adequate for most applications.

Download  source code version 1.1

C source code for compiler (run on PC, test compile with Turbo C v 2.0) and interpreter (tested on Solaris)