Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I decompile Perl 6?

Tags:

raku

We're supposed to be able to do this somehow. I think I've seen it somewhere, but I can't find what I think I remember. Mostly I want to see how the compiler interprets code.

Along with decompiling, is there a way to watch what it's doing as it compiles? I think seeing what's it's trying to do and where might be easier than trying to understand some of its error messages. And, decompiling a program can undo all the shorthand and cleverness to elucidate what's actually happening.

like image 958
brian d foy Avatar asked Jul 04 '17 01:07

brian d foy


1 Answers

I'm not sure why you would want to do it. Also, the compilation result is backend dependent, and you did not specify a backend. Anyway, with the --target= parameter, you can get intermediate results. The most useful are:

$ perl6 --target=parse -e 'say "foo"'
- statementlist: say "foo"
  - statement: 1 matches
    - EXPR: say "foo"
      - args:  "foo"
        - arglist: "foo"
          - EXPR: "foo"
            - value: "foo"
              - quote: "foo"
                - nibble: foo
      - longname: say
        - name: say
          - identifier: say
          - morename:  isa NQPArray
        - colonpair:  isa NQPArray

--target=parse shows the immediate result of the parse.

$ perl6 --target=ast -e 'say "foo"'
- QAST::CompUnit  :W<?> :UNIT<?>
  [pre_deserialize]
    - QAST::Stmt 
      - QAST::Stmt 
        - QAST::Op(loadbytecode)
          - QAST::VM 
            [jvm]
              - QAST::SVal(ModuleLoader.class) 
            [moar]
              - QAST::SVal(ModuleLoader.moarvm) 
        - QAST::Op(callmethod load_module) 
*snip*

--target=ast shows the Abstract Syntax Trees.

$ perl6 --target=mast -e 'say "foo"'
MAST::Frame name<<unit-outer>>, cuuid<2>
  Local types: 0<obj>, 1<obj>, 2<obj>, 3<obj>, 
  Outer: <none>
  Instructions:
  [0] MAST::Op getcode
    MAST::Local index<3>
    MAST::Frame name<<unit>>, cuuid<1>
  [1] MAST::Op capturelex
    MAST::Local index<3>
  [2] MAST::Op getcode
    MAST::Local index<1>
    MAST::Frame name<<unit>>, cuuid<1>
  [3] MAST::Op takeclosure
*snip*

And --target=mast shows the actual byte code that is being generated, in this case for the MoarVM backend.

like image 75
Elizabeth Mattijsen Avatar answered Oct 27 '22 02:10

Elizabeth Mattijsen