Board index » delphi » Jump tables in Borland Pascal 7.0

Jump tables in Borland Pascal 7.0

I'm currently writing a Scheme interpreter in Borland Pascal 7.0. (Scheme is
a LISP dialect, not entirely functional/applicative (ie: pure), but close. It
has many theoretically interesting (and useful) features, besides being pure
and simple in design. For those who want more info, see my homepage at
http://www.ifi.uio.no/~larsga (under 'diverse linker' and then
'programmeringssprak'.)

This interpreter compiles Scheme expressions into instructions for a virtual
machine of my own design. Both the interpreter and the machine are written
entirely in Pascal. The procedure of the machine (the machine is an object)
looks like this:

procedure TVirtMaskin.Kjoer; { Kjoer means run }
VAR
  Stopp : boolean;
  Instr : byte;
BEGIN
  REPEAT
    Instr:= <get next instruction>;

    Case Instr of
    <Instr 1>: <Execute>;
    <Instr 2>: <Execute>;
    .
    .
    .
    ELSE
      <Error, unknown instruction>
    END;
  UNTIL Stopp;
END;

The problem is this: I know from discussions on this group that the compiled
code for this procedure does not use a jump table, but for speed I would very
much like to use a jump table. What I had imagined was inserting a piece of
assembly code where the Case Instr line is now that used a jump table to
jump to the right instruction. However, I can see no way whatsoever to
achieve this. Can anyone else?

As you can probably imagine, this improvement could result in a significant
speed-up for my interpreter, especially as the virtual machine has around 35
instructions at the moment, and I can foresee a need for many many more.
(Excactly how many depends on how much I'll include in my version of Scheme.)

So if anyone could help me out here I'll be extremely grateful, but as you
can see, my problem is that the main part of the procedure (the executing
part) has to be Pascal, while the part that decodes the instructions can be
anything. I know I can use a procedural table and call the right procedure,
but I think that will slow the program even more (because of the overhead
required for procedure calls), at least until I have very many instructions.

Well, enough of this rambling.

Thanks a lot to anyone who helps,
--Lars M.

 

Re:Jump tables in Borland Pascal 7.0


Lars Marius Garshol (lar...@ifi.uio.no) wrote:

...munched... (and reformatted)...
:
: The problem is this: I know from discussions on this group that the
: compiled code for this procedure does not use a jump table, but for
: speed I would very much like to use a jump table. What I had imagined
: was inserting a piece of assembly code where the Case Instr line is
: now that used a jump table to jump to the right instruction. However,
: I can see no way whatsoever to achieve this. Can anyone else?

  Take a look a Procedural-type constants, and procedure types.  To build a
jump table, do something like the following: (Have fun)
--
{$F+ = Note, procedural types require "far calls" }

type
  proc = procedure;               { Note: no parameters!   }
  parr = array[ 0..3 ] of proc;   { This is the jump table }

procedure zero;  begin  WriteLn( 'zero'   );  end;
procedure one;   begin  WriteLn( 'one'    );  end;
procedure two;   begin  WriteLn( 'two'    );  end;
procedure three; begin  WriteLn( 'three'  );  end;

const
  jTbl : parr = ( zero, one, two, three );

var
  ix : Word;

begin
  for ix := 0 to 3 do
    jTbl[ ix ];
end.

--
C-Code, C-Code Run, Run Code Run, Please!

Bob Gibson -- gib...@netcom.com

Other Threads