Here are three short examples of TVI in action.  They are fib.tvi, fib_opt.tvi and fib_nopt.tvi.

The file fib.tvi is a hand coded program for calculating numbers in the Fibonacci sequence using recursion.  Parameters to the
Fib function are passed by reference as your compiler will have to.

The files fib_opt.tvi and fib_nopt.tvi give some examples of what machine generated code will actually look like.  (
Yes, I sat down and wrote your compiler…)  However, the semantic actions were not completely worked out when I wrote my compiler so the code here will not be exactly the same as the type of code your compilers will generate.  In particular, I pass parameters by value rather than by reference, and my compiler reserves address 0 for use for function results rather than generating a temporary each time (Actually, a temporary still gets generated to hold the function result, but the temp is generated in a different section of the compiler so the compiler needs to be able to find the function result quickly, it does this by always storing it in a known location, address 0).

The sample Pascal code that was used to generate fib_opt.tvi and fib_nopt.tvi can be found here.  You will notice that I also took one other liberty when implementing my compiler.  Functions can store their result in the system variable
result rather than the function name.  That is, rather than doing

Function Sum(a, b,: Integer):  Integer;
begin
    Sum := a + b;
end

my compiler allows the syntax

Function Sum(a, b,: Integer):  Integer;
begin
    result := a + b;
end

This is identical to the syntax used in Borland (now Inprise) Delphi, which uses a Pascal like language and is THE BEST thing to ever happen to Windows programming.  (Delphi is what Visual Basic would have been if Microsoft had any decent programmers on staff, VC++ comes in as a distant wanna-be.)

Back to top

To contact us:

© Copyright 1999 Vassar College
Poughkeepsie, New York

Web pages  by
Keith Suderman Consulting