C-TinyCompiler
view release on metacpan or search on metacpan
lib/C/TinyCompiler.pm view on Meta::CPAN
=head1 COMPILE METHODS
These are methods related to compiling your source code. Apart from C<compile>,
you need not worry about these methods unless you are trying to create a C::TinyCompiler
package.
=head2 compile
Concatenates the text of the three code sections, jit-compiles them, applies all
symbols from the included packages, and relocates the code so that symbols can
be retrieved. In short, this is the transformative step that converts your code
from ascii into machine.
This step does far more than simply invoke libtcc's compile function. At the
time of writing, tcc only supports a single uncompiled compiler state at a time.
To properly handle this, C::TinyCompiler defers creating the actuall TCCState
object as long as possible. Calling the C<compile> method on your compiler
context actually performs these steps:
=over
lib/C/TinyCompiler.pm view on Meta::CPAN
Packages can perform preprocessing on the compiler context (and in particular,
the code strings) just before the actual compilation step. This allows them to
dynmically add or remove elements to your code, like source-filters. Or they
could hold off to perform other changes to the compiler context until just
before the compilation step, although this is generally not needed.
=item 4. Code assembly and compilation
The code is assembled and compiled.
=item 5. Apply symbols and relocate the machine code
Symbols (such as dynamically loaded functions) are applied, the final machine
code is relocated, and the memory pages holding that code are marked as
executable.
=back
This means that nearly all of the interaction with libtcc itself is deferred
until you call this function. As each of those interactions could encounter
trouble, this function may croak for many reasons.
=over
lib/C/TinyCompiler.pm view on Meta::CPAN
If the reported line numbers do not help, consider using L</line_numbers> to
improve line number reporting.
=item Error adding symbol(s): <message>
If you specify symbols that have already been defined elsewhere, the compiler
will thwart your attempts with this message. Make sure that you have not defined
a like-named symbol already. In particular, be sure not to define a symbol that
was defined already by one of your packages.
=item Unable to relocate: <message>
The last step in converting your C code to machine-executable code is relocating
the bytecode. This could fail, though I do not understand compilers well enough
to explain why. If I had to guess, I would say you probably ran out of memory.
(Sorry I cannot provide more insight into how to fix this sort of problem.
Feedback for a better explanation would be much appreciated. :-)
=back
=cut
sub compile {
lib/C/TinyCompiler.pm view on Meta::CPAN
# Apply the pre-compiled symbols (function pointers, etc):
while (my ($package, $options) = each %{$self->{applied_package}}) {
$package->apply_symbols($self, @$options);
}
# Apply any other symbols that were added:
$self->_add_symbols(%{$self->{symbols}});
$self->report_if_trouble("adding symbol(s): MESSAGE");
# Relocate
eval {
$self->_relocate;
1;
} or do {
# We ran into a problem! Report the relocation issue, if known:
$self->report_if_trouble("relocating: MESSAGE");
# Report an unknown relocation issue if not known:
croak("C::TinyCompiler weird internal error: Unable to relocate for unknown reasons");
};
# Mark the compiler as post-compile
$self->{has_compiled} = 1;
}
=head2 add_symbols
Adds symbols to a compiler context. This function expects the symbols as
lib/C/TinyCompiler.pm view on Meta::CPAN
If you fail to provide key/value pairs, this function will croak saying
You must supply key => value pairs to add_symbols
=cut
sub add_symbols {
my $self = shift;
# working here - not sure if it's safe to add symbols after relocation.
croak('You must supply key => value pairs to add_symbols')
unless @_ % 2 == 0;
my %symbols = @_;
while (my ($symbol, $pointer) = each %symbols) {
# Track the symbols, warning on redefinitions
warnings::warnif("Redefining $symbol")
if exists $self->{symbols}->{$symbol};
$self->{symbols}->{$symbol} = $pointer;
lib/C/TinyCompiler.xs view on Meta::CPAN
croak("INTERNAL ERROR: _add_symbols should only get key => value pairs\n");
}
int i;
for (i = 1; i < items; i += 2) {
symbol_name = SvPVbyte_nolen(ST(i));
symbol_ptr = INT2PTR(void*, SvIV(ST(i+1)));
tcc_add_symbol(state, symbol_name, symbol_ptr);
}
void
_relocate(state)
TCCStateObj * state
CODE:
/* Relocate and croak if error */
int ret = tcc_relocate(state, TCC_RELOCATE_AUTO);
if (ret < 0) croak("Relocation error\n");
############ Post-Compiler ############
void
_call_void_function(state, func_name)
TCCStateObj * state
const char * func_name
CODE:
/* Get a pointer to the function */
my_void_func p_func = (my_void_func)tcc_get_symbol(state, func_name);
magic_sizepack|||
magic_wipepack|||
make_matcher|||
make_trie_failtable|||
make_trie|||
malloc_good_size|||n
malloced_size|||n
malloc||5.007002|n
markstack_grow|||
matcher_matches_sv|||
mayberelocate|||
measure_struct|||
memEQs|5.009005||p
memEQ|5.004000||p
memNEs|5.009005||p
memNE|5.004000||p
mem_collxfrm|||
mem_log_common|||n
mess_alloc|||
mess_nocontext|||vn
mess_sv||5.013001|
( run in 1.459 second using v1.01-cache-2.11-cpan-71847e10f99 )