Alien-LibJIT
view release on metacpan or search on metacpan
libjit/doc/libjit.texi view on Meta::CPAN
and @code{insn_add} instead in this example and the result would have
been the same.
Now that we have our @code{mul_add_function} class, we can create
an instance of the function and apply it as follows:
@example
jit_context context;
mul_add_function mul_add(context);
jit_int arg1 = 3;
jit_int arg2 = 5;
jit_int arg3 = 2;
jit_int args[3];
args[0] = &arg1;
args[1] = &arg2;
args[2] = &arg3;
mul_add.apply(args, &result);
@end example
@noindent
@xref{C++ Interface}, for more information on the @code{libjitplus}
library.
@c -----------------------------------------------------------------------
@node Tutorial 5, Dynamic Pascal, Tutorial 4, Tutorials
@section Tutorial 5 - gcd, with tail calls
@cindex gcd with tail calls
Astute readers would have noticed that Tutorial 2 included two instances
of "tail calls". That is, calls to the same function that are immediately
followed by a @code{return} instruction.
Libjit can optimize tail calls if you provide the @code{JIT_CALL_TAIL}
flag to @code{jit_insn_call}. Previously, we used the following code
to call @code{gcd} recursively:
@example
temp3 = jit_insn_call
(function, "gcd", function, 0, temp_args, 2, 0);
jit_insn_return(function, temp3);
@end example
@noindent
In Tutorial 5, this is modified to the following:
@example
jit_insn_call(function, "gcd", function, 0, temp_args, 2, JIT_CALL_TAIL);
@end example
There is no need for the @code{jit_insn_return}, because the call
will never return to that point in the code. Behind the scenes,
@code{libjit} will convert the call into a jump back to the head
of the function.
Tail calls can only be used in certain circumstances. The source
and destination of the call must have the same function signatures.
None of the parameters should point to local variables in the current
stack frame. And tail calls cannot be used from any source function
that uses @code{try} or @code{alloca} statements.
Because it can be difficult for @code{libjit} to determine when these
conditions have been met, it relies upon the caller to supply the
@code{JIT_CALL_TAIL} flag when it is appropriate to use a tail call.
@c -----------------------------------------------------------------------
@node Dynamic Pascal, Initialization, Tutorial 5, Tutorials
@section Dynamic Pascal - A full JIT example
@cindex Dynamic Pascal
This @code{libjit/dpas} directory contains an implementation of
"Dynamic Pascal", or "dpas" as we like to call it. It is provided
as an example of using @code{libjit} in a real working environment.
We also use it to write test programs that exercise the JIT's capabilities.
Other Pascal implementations compile the source to executable form,
which is then run separately. Dynamic Pascal loads the source code
at runtime, dynamically JIT'ing the program as it goes. It thus has
a lot in common with scripting languages like Perl and Python.
If you are writing a bytecode-based virtual machine, you would use
a similar approach to Dynamic Pascal. The key difference is that
you would build the JIT data structures after loading the bytecode
rather than after parsing the source code.
To run a Dynamic Pascal program, use @code{dpas name.pas}. You may also
need to pass the @code{-I} option to specify the location of the system
library if you have used an @code{import} clause in your program. e.g.
@code{dpas -I$HOME/libjit/dpas/library name.pas}.
@noindent
This Pascal grammar is based on the EBNF description at the following URL:
@uref{http://www.cs.qub.ac.uk/~S.Fitzpatrick/Teaching/Pascal/EBNF.html}
@noindent
There are a few differences to "Standard Pascal":
@enumerate
@item
Identifiers are case-insensitive, but case-preserving.
@item
Program headings are normally @code{program Name (Input, Output);}. This can
be abbreviated to @code{program Name;} as the program modifiers are ignored.
@item
Some GNU Pascal operators like @code{xor}, @code{shl}, @code{@@}, etc
have been added.
@item
The integer type names (@code{Integer}, @code{Cardinal}, @code{LongInt}, etc)
follow those used in GNU Pascal also. The @code{Integer} type is always
32-bits in size, while @code{LongInt} is always 64-bits in size.
@item
The types @code{SysInt}, @code{SysCard}, @code{SysLong}, @code{SysLongCard},
@code{SysLongestInt}, and @code{SysLongestCard} are guaranteed to be the
( run in 0.399 second using v1.01-cache-2.11-cpan-df04353d9ac )