Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am interested in your take on this article http://tldp.org/HOWTO/Unix-and-Internet-Fundamentals-HOWTO/languages.html which includes perl as a language which compiles to an intermediary p-code. I always thought of perl as a purely interpreted language.

Also, does anyone have any flow-diagrams of how source code makes it to machine code for each category (compiled, JIT, & interpreted)?

Many thanks!

Replies are listed 'Best First'.
Re: is perl p-code?
by LanX (Saint) on Mar 31, 2011 at 14:39 UTC
    >is perl p-code?

    yes they are called opcodes!

    This example shows with the help of B::Terse how the sub take is called with 1+2 as parameter.

    > perl -MO=Terse -e 'take(1+2)' LISTOP (0x976cdf0) leave [1] OP (0x9777078) enter COP (0x97542e8) nextstate UNOP (0x975fbb0) entersub [3] UNOP (0x975c8b8) null [142] OP (0x9754818) pushmark SVOP (0x975ca50) const [4] IV (0x9758ea8) 3 UNOP (0x975c878) null [17] PADOP (0x975c938) gv GV (0x9758eb8) *take -e syntax OK

    And with B::Deparse you can even back-engineer an equivalent Perl code:

    perl -MO=Deparse -e 'take(1+2)' take(3);

    > I always thought of Perl as a purely interpreted language.

    No that is (was) TCL's problem.

    > Also, does anyone have any flow-diagrams of how source code makes it to machine code for each category (compiled, JIT, & interpreted)?

    Well this is a Perl board! Look at BEGIN, UNITCHECK, CHECK, INIT and END for compilation phases of Perl.

    Cheers Rolf

Re: is perl p-code?
by ikegami (Patriarch) on Mar 31, 2011 at 16:51 UTC

    According to wikipedia, p-code is "the assembly language of a hypothetical CPU".

    Perl does compile to an intermediary form, and it does have the appearance of assembly language. Perl never serialised the opcodes into a binary for that hypothetical CPU, but the possibility is there.

    I always thought of perl as a purely interpreted language.

    It's definitely not purely interpreted.

    Also, does anyone have any flow-diagrams of how source code makes it to machine code for each category (compiled, JIT, & interpreted)?

    Perl code is compiled into a tree of Perl opcode. Optimisations are applied, then the tree is turned into a link list. (I might have the order wrong.) The product looks like:

    >perl -MO=Concise,-exec -E"say 'Hello, World' for 1..3" 1 <0> enter 2 <;> nextstate(main 47 -e:1) v:%,{,2048 3 <;> nextstate(main 47 -e:1) v:%,{,2048 4 <0> pushmark s 5 <$> const[IV 1] s 6 <$> const[IV 3] s 7 <#> gv[*_] s 8 <{> enteriter(next->c last->f redo->9) lKS/8 d <0> iter s e <|> and(other->9) vK/1 9 <0> pushmark s a <$> const[PV "Hello, World"] s b <@> say vK c <0> unstack v goto d f <2> leaveloop vK/2 g <@> leave[1 ref] vKP/REFC -e syntax OK

    The opcodes are then executed using the following loop in run.c:

    while ((PL_op = op = op->op_ppaddr(aTHX))) { }

    (That's the whole thing.)

    There's no jit compiling yet. Work is being down to provide the option to compile Perl5 to LLVM bytecode. This, in turn, can be jit compiled into machine code. This is the same approach Java uses.