Skip to content

Commit

Permalink
B-C-1.04_05
Browse files Browse the repository at this point in the history
git-svn-id: http://perl-compiler.googlecode.com/svn/trunk@6 ed534f1a-1453-0410-ab30-dfc593a8b23c
  • Loading branch information
Reini Urban committed Jul 28, 2008
1 parent 67b941c commit bb57d20
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 37 deletions.
18 changes: 12 additions & 6 deletions B/Bytecode.pm
Expand Up @@ -738,13 +738,15 @@ sub compile {
*newasm = *endasm = sub { };
*asm = sub { print " @_\n" };
*nice = sub ($) { print "\n@_\n" };
} elsif (/^-v/) {
*nice = sub ($) { print STDERR "@_\n" };
} elsif (/^-H/) {
require ByteLoader;
$head = "#! $^X
use ByteLoader $ByteLoader::VERSION;
";
#FIXME!
undef $head;
#undef $head;
} elsif (/^-k/) {
keep_syn;
} elsif (/^-o(.*)$/) {
Expand Down Expand Up @@ -841,16 +843,15 @@ later by the ByteLoader module and executed as a regular Perl script.
=over 4
=item B<-H>
prepend a C<use ByteLoader VERSION;> line to the produced bytecode.
=item B<-b>
Save all the BEGIN blocks. Normally only BEGIN blocks that C<require>
other files (ex. C<use Foo;>) are saved.
=item B<-H>
prepend a C<use ByteLoader VERSION;> line to the produced bytecode.
Does not work currently.
=item B<-k>
keep the syntax tree - it is stripped by default.
Expand All @@ -864,6 +865,11 @@ put the bytecode in <outfile> instead of dumping it to STDOUT.
scan the script for C<# line ..> directives and for <goto LABEL>
expressions. When gotos are found keep the syntax tree.
=item B<-S>
Output assembler source rather than piping it
through the assembler and outputting bytecode.
=back
=head1 KNOWN BUGS
Expand Down
2 changes: 1 addition & 1 deletion B/C.pm
Expand Up @@ -8,7 +8,7 @@

package B::C;

our $VERSION = '1.04_04';
our $VERSION = '1.04_05';

package B::C::Section;

Expand Down
12 changes: 10 additions & 2 deletions ByteLoader/bytecode.h
Expand Up @@ -201,7 +201,6 @@ typedef HEK *hekindex;
if(av_len((AV*) PL_regex_pad[0]) > -1) { \
repointer = av_pop((AV*)PL_regex_pad[0]); \
cPMOPx(o)->op_pmoffset = SvIV(repointer); \
SvREPADTMP_off(repointer); \
sv_setiv(repointer,PTR2IV(rx)); \
} else { \
repointer = newSViv(PTR2IV(rx)); \
Expand Down Expand Up @@ -291,6 +290,10 @@ typedef HEK *hekindex;
#define set_clonex(fp)
#endif

#ifndef PL_preprocess
#define PL_preprocess 0
#endif

#define BSET_data(dummy,arg) \
STMT_START { \
GV *gv; \
Expand All @@ -305,7 +308,7 @@ typedef HEK *hekindex;
set_clonex(PL_RSFP); \
/* Mark this internal pseudo-handle as clean */ \
IoFLAGS(GvIOp(gv)) |= IOf_UNTAINT; \
if (PL_preprocess) \
if ((PERL_VERSION < 11) && PL_preprocess) \
IoTYPE(GvIOp(gv)) = IoTYPE_PIPE; \
else if ((PerlIO*)PL_RSFP == PerlIO_stdin()) \
IoTYPE(GvIOp(gv)) = IoTYPE_STD; \
Expand Down Expand Up @@ -479,4 +482,9 @@ typedef HEK *hekindex;
if (sz != PTRSIZE) { \
HEADER_FAIL("different PTRSIZE"); \
} \
BGET_strconst(str); /* 12345678 */ \
if (strNE(str, "12345678")) { \
HEADER_FAIL2("cannot yet convert different byteorders (want %s, you have %s)", \
"12345678", str); \
} \
} STMT_END
9 changes: 7 additions & 2 deletions ByteLoader/byterun.c
Expand Up @@ -55,7 +55,7 @@ byterun(pTHX_ struct byteloader_state *bstate)
register int insn;
U32 isjit = 0;
U32 ix;
SV *specialsv_list[6];
SV *specialsv_list[7];

BYTECODE_HEADER_CHECK; /* croak if incorrect platform, set isjit if PLJC magic header */
if (isjit) {
Expand All @@ -65,6 +65,10 @@ byterun(pTHX_ struct byteloader_state *bstate)
bstate->bs_obj_list_fill = 31;
bstate->bs_obj_list[0] = NULL; /* first is always Null */
bstate->bs_ix = 1;
DEBUG_l( Perl_deb(aTHX_ "(bstate.bs_fdata.idx %d)\n", bstate->bs_fdata->idx));
DEBUG_l( Perl_deb(aTHX_ "(bstate.bs_fdata.next_out %d)\n", bstate->bs_fdata->next_out));
DEBUG_l( Perl_deb(aTHX_ "(bstate.bs_fdata.datasv %p:\"%s\")\n", bstate->bs_fdata->datasv,
SvPV_nolen(bstate->bs_fdata->datasv)));

specialsv_list[0] = Nullsv;
specialsv_list[1] = &PL_sv_undef;
Expand All @@ -75,7 +79,8 @@ byterun(pTHX_ struct byteloader_state *bstate)
specialsv_list[6] = (SV*)pWARN_STD;

while ((insn = BGET_FGETC()) != EOF) {
switch (insn) {
DEBUG_l( Perl_deb(aTHX_ "(insn %d)\n", insn));
switch (insn) {
case INSN_COMMENT: /* 35 */
{
comment_t arg;
Expand Down
2 changes: 1 addition & 1 deletion ByteLoader/jitrun.c
Expand Up @@ -94,7 +94,7 @@ jitrun(pTHX_ struct byteloader_state *bstate)

jit_func bc_func = (jit_func) (jit_set_ip(codeBuffer).iptr); /* Function ptr */
#ifdef DEBUGGING
disassemble(stderr, codeBuffer, jit_get_ip().ptr);
//disassemble(stderr, codeBuffer, jit_get_ip().ptr);
#endif
jit_flush_code(codeBuffer, jit_get_ip().ptr);

Expand Down
5 changes: 3 additions & 2 deletions ByteLoader/jitrun.h
Expand Up @@ -59,8 +59,9 @@
*/

/* DATA handle reader */
int jit_getc(struct byteloader_fdata *);
int jit_read(struct byteloader_fdata *, char *, size_t, size_t);
/* int bl_getc(struct byteloader_fdata *);
int bl_read(struct byteloader_fdata *, char *, size_t, size_t);
*/

#include <lightning.h>

Expand Down
11 changes: 11 additions & 0 deletions Changes
@@ -1,6 +1,17 @@
Started with B-C-1.04_01. The perl compiler was previously in CORE.
TODO: Try to get info about earlier versions.

1.04_05 2008-02-18 rurban
* added t/test.pl and t/test*.sh to MANIFEST.
* fixed ByteLoader reading from the <DATA> filter.
* fixed -H .plc header parsing
* updated Bytecode options in NOTES and pod
* added -O=Bytecode,-v option
* fixed PL_preprocess and SvREPADTMP_off for 5.11

1.04_04 2008-02-08 rurban
* added jitcompiler macros

1.04_03 2008-02-05 rurban
* added Jit and Asm layout, fixed Makefile deps

Expand Down
3 changes: 3 additions & 0 deletions MANIFEST
Expand Up @@ -58,6 +58,9 @@ t/c.t
t/cc.t
t/o.t
t/stash.t
t/test.pl
t/testplc.sh
t/testc.sh
TESTS
Todo
META.yml Module meta-data (added by MakeMaker)
2 changes: 1 addition & 1 deletion META.yml
@@ -1,6 +1,6 @@
--- #YAML:1.0
name: B-C
version: 1.04_04
version: 1.04_05
abstract: ~
license: ~
author: ~
Expand Down
8 changes: 6 additions & 2 deletions Makefile.PL
@@ -1,15 +1,15 @@
use ExtUtils::MakeMaker;
use Config;
use File::Spec;
use 5.009005;
#use 5.009005;

my $core = grep { $_ eq 'PERL_CORE=1' } @ARGV;

WriteMakefile(
NAME => "B::C",
VERSION_FROM => "B/C.pm",
# 'scripts/assemble','scripts/disassemble',
PL_FILES => {'script/perlcc.PL' => 'bin/perlcc' },
PL_FILES => {'script/perlcc.PL' => '$(INST_BIN)/perlcc' },
PREREQ_PM => {
'B::Concise' => '0.74',
'B' => '1.17',
Expand All @@ -28,6 +28,10 @@ sub headerpath {

package MY;

sub x_libscan {
return 0 unless $_[1] =~ /\.pm$/;
}

sub post_constants {
"\nLIBS = $Config::Config{libs}\n"
}
Expand Down
26 changes: 22 additions & 4 deletions NOTES
Expand Up @@ -2,6 +2,8 @@ C backend invocation
If there are any non-option arguments, they are taken to be
names of objects to be saved (probably doesn't work properly yet).
Without extra arguments, it saves the main program.
-q Be quiet. STDOUT goes to $O::BEGIN_output
-qq Be very quiet. Also suppress "Syntax OK"
-ofilename Output to filename instead of STDOUT
-v Verbose (currently gives a few compilation statistics)
-- Force end of options
Expand Down Expand Up @@ -35,7 +37,10 @@ Examples
CC backend invocation
If there are any non-option arguments, they are taken to be names of
subs to be saved. Without extra arguments, it saves the main program.
-q Be quiet. STDOUT goes to $O::BEGIN_output
-qq Be very quiet. Also suppress "Syntax OK"
-ofilename Output to filename instead of STDOUT
NYI -v Be verbose.
-- Force end of options
-uPackname Force apparently unused subs from package Packname to
be compiled. This allows programs to use eval "foo()"
Expand Down Expand Up @@ -101,7 +106,10 @@ Bytecode backend invocation
If there are any non-option arguments, they are taken to be
names of objects to be saved (probably doesn't work properly yet).
Without extra arguments, it saves the main program.
-q Be quiet. STDOUT goes to $O::BEGIN_output
-qq Be very quiet. Also suppress "Syntax OK"
-ofilename Output to filename instead of STDOUT.
NYI -v Be verbose.
-- Force end of options.
-f Force optimisations on or off one at a time.
Each can be preceded by no- to turn the option off.
Expand All @@ -121,24 +129,34 @@ Bytecode backend invocation
run-time but leaving them out will make it impossible
to recompile or disassemble the resulting program.
It will also stop "goto label" statements from working.
-On Optimisation level (n = 0, 1, 2, ...). -O means -O1.
NYI -On Optimisation level (n = 0, 1, 2, ...). -O means -O1.
-O1 sets -fcompress-nullops -fomit-sequence numbers.
-O6 adds -fstrip-syntax-tree.
-D Debug options (concat or separate flags like perl -D)
NYI -D Debug options (concat or separate flags like perl -D)
o OPs, prints each OP as it's processed.
b print debugging information about bytecompiler progress
a tells the assembler to include source assembler lines
in its output as bytecode comments.
C prints each CV taken from the final symbol tree walk.
-S Output assembler source rather than piping it
through the assembler and outputting bytecode.
-m Compile as a module rather than a standalone program.
-H add #! perl shebang header
-s scan and keep keep syntax tree if goto op found.
scan the script for C<# line ..> directives and for <goto LABEL>
expressions. When gotos are found keep the syntax tree.
-b Save all the BEGIN blocks. Normally only BEGIN blocks that require
other files (ex. use Foo;) are saved.
-k keep syntax tree to disassemble the plc.
it is stripped by default.
-TI testing, dump the @INC av
-TF file testing, sets COP::file
-m Compile as a module rather than a standalone program.
Currently this just means that the bytecodes for
initialising main_start, main_root and curpad are
omitted.

Example
perl -MO=Bytecode,-O6,-o,foo.plc foo.pl
perl -MO=Bytecode,-O6,-H,-ofoo.plc foo.pl

perl -MO=Bytecode,-S foo.pl > foo.S
assemble foo.S > foo.plc
Expand Down
2 changes: 1 addition & 1 deletion Todo
Expand Up @@ -16,7 +16,6 @@ CvOUTSIDE for ordinary subs
DATA filehandle for standalone Bytecode program (easy)
DATA filehandle for multiple bytecode-compiled modules (harder)
DATA filehandle for C-compiled program (yet harder)
parse -H Bytecode header

* Features

Expand All @@ -31,6 +30,7 @@ module dependencies
new Asm and JIT backend, PLJC ByteLoader magic

* Optimisations

collapse LISTOPs to UNOPs or BASEOPs
compile-time qw(), constant subs
global analysis of variables, type hints etc.
Expand Down
11 changes: 9 additions & 2 deletions bytecode.pl
Expand Up @@ -110,7 +110,9 @@ package B::Asmdata;
register int insn;
U32 isjit = 0;
U32 ix;
SV *specialsv_list[6];
EOT
printf BYTERUN_C " SV *specialsv_list[%d];\n", scalar @specialsv_name;
print BYTERUN_C <<'EOT';
BYTECODE_HEADER_CHECK; /* croak if incorrect platform, set isjit if PLJC magic header */
if (isjit) {
Expand All @@ -120,6 +122,10 @@ package B::Asmdata;
bstate->bs_obj_list_fill = 31;
bstate->bs_obj_list[0] = NULL; /* first is always Null */
bstate->bs_ix = 1;
DEBUG_l( Perl_deb(aTHX_ "(bstate.bs_fdata.idx %d)\n", bstate->bs_fdata->idx));
DEBUG_l( Perl_deb(aTHX_ "(bstate.bs_fdata.next_out %d)\n", bstate->bs_fdata->next_out));
DEBUG_l( Perl_deb(aTHX_ "(bstate.bs_fdata.datasv %p:\"%s\")\n", bstate->bs_fdata->datasv,
SvPV_nolen(bstate->bs_fdata->datasv)));
EOT

Expand All @@ -130,7 +136,8 @@ package B::Asmdata;
print BYTERUN_C <<'EOT';
while ((insn = BGET_FGETC()) != EOF) {
switch (insn) {
DEBUG_l( Perl_deb(aTHX_ "(insn %d)\n", insn));
switch (insn) {
EOT


Expand Down
30 changes: 19 additions & 11 deletions jitcompiler.pl
Expand Up @@ -4,24 +4,31 @@
=head1 JIT or ASM?
Two possible native code attempts:
Possible native code attempts:
1. Use B<GNU lightning> to compile to jit at eval-time, and
jitrun the whole codebuffer, the appended insns instead
of looping through the opcode tree.
Possibly dump the jitted codebuffer as PLJC .plc files, to save
next startup parse time.
1. Use some jit library, such as B<GNU lightning>, to compile to jit
at eval-time, and jitrun the whole codebuffer, the appended insns
instead of looping through the opcode tree. Possibly dump the
jitted codebuffer as PLJC .plc files, to save next startup parse
time.
This must be done with jit macro trickery on the C level.
2. Use a self-written B<B::Asm> library to dump architectur specific
LLVM has some nice SSA optimizations.
See L<http://llvm.org/ProjectsWithLLVM/2002-Spring-CS497CZ-Jello.pdf>
2. Use perl6 jit.
http://www.parrotcode.org/docs/jit.html#EXAMPLE
This already has optimizations for immediate and strict integer ops.
3. Use a self-written B<B::Asm> library to dump architectur specific
asm insns directly.
Either dump the jitted codebuffer as PLJC .plc files, or construct
the necessary OS headers directly. For writing the PE/COFF format
see e.g. my L<C::DynaLib> pl2exe or Audreys L<Win32::Exe>, for writing
Elf see e.g. L<GNU libjit>.
This could be done on a higher Perl level, and avoids the lightning
This could be done on a higher Perl level, and avoids the C-level
macro abstraction.
=head1 OPTIMIZATIONS
Expand Down Expand Up @@ -166,7 +173,7 @@ BEGIN
jit_func bc_func = (jit_func) (jit_set_ip(codeBuffer).iptr); /* Function ptr */
#ifdef DEBUGGING
disassemble(stderr, codeBuffer, jit_get_ip().ptr);
//disassemble(stderr, codeBuffer, jit_get_ip().ptr);
#endif
jit_flush_code(codeBuffer, jit_get_ip().ptr);
Expand Down Expand Up @@ -235,8 +242,9 @@ BEGIN
*/
/* DATA handle reader */
int jit_getc(struct byteloader_fdata *);
int jit_read(struct byteloader_fdata *, char *, size_t, size_t);
/* int bl_getc(struct byteloader_fdata *);
int bl_read(struct byteloader_fdata *, char *, size_t, size_t);
*/
#include <lightning.h>
Expand Down
4 changes: 2 additions & 2 deletions script/perlcc
@@ -1,5 +1,5 @@
#!/usr/local/bin/perl5.10.0-nt
eval 'exec /usr/local/bin/perl5.10.0-nt -S $0 ${1+"$@"}'
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if $running_under_some_shell;
--$running_under_some_shell;

Expand Down

0 comments on commit bb57d20

Please sign in to comment.