Forum: Ruby Running ruby1.9 bytecode

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2009-04-06 14:49
(Received via mailing list)
Hi all,

I just found out about the VM::InstructionSequence.compile method for
turning Ruby code strings into ruby1.9 bytecode. Is there a standard
method
of de/serializing this bytecode to/from files, and is there a way of
running
bytecode directly? I'm looking at generating bytecode from another
language
to get it to run on Ruby.
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2009-04-06 20:59
(Received via mailing list)
Originally posted to Ruby Talk, trying my luck here to see if it gets a
response:

---------- Forwarded message ----------
From: James Coglan <jcoglan@googlemail.com>
Date: 2009/4/6
Subject: Running ruby1.9 bytecode
To: ruby-talk ML <ruby-talk@ruby-lang.org>


Hi all,

I just found out about the VM::InstructionSequence.compile method for
turning Ruby code strings into ruby1.9 bytecode. Is there a standard
method
of de/serializing this bytecode to/from files, and is there a way of
running
bytecode directly? I'm looking at generating bytecode from another
language
to get it to run on Ruby.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-04-06 21:29
(Received via mailing list)
On Mon, Apr 6, 2009 at 2:48 PM, James Coglan <jcoglan@googlemail.com>
wrote:
> Hi all,
>
> I just found out about the VM::InstructionSequence.compile method for
> turning Ruby code strings into ruby1.9 bytecode. Is there a standard method
> of de/serializing this bytecode to/from files, and is there a way of running
> bytecode directly? I'm looking at generating bytecode from another language
> to get it to run on Ruby.
Quoting from the Pickaxe book
no, because the bytecode verifier is not ready yet, that seems the
only missing bit.
HTH
Robert
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-04-06 22:37
(Received via mailing list)
On Mon, Apr 6, 2009 at 3:27 PM, Robert Dober <robert.dober@gmail.com>
wrote:

> language
> > to get it to run on Ruby.
> Quoting from the Pickaxe book
> no, because the bytecode verifier is not ready yet, that seems the
> only missing bit.
> HTH
> Robert
>

Well that's part of it. But, if by "generating bytecode from another
language" means, for instance, executing JVM bytecodes, I don't think it
will fly. The term bytecode here is being used rather generically. The
YARV
"bytecodes" aren't the same as the JVM bytecodes, any more than
Smalltalk-80
bytecodes are the same as either.

It's like the difference between an Intel and Motorola processor, both
have
instruction sets with binary representations, but those instruction sets
aren't necessarily interchangeable.

Getting another language to produce YARV bytecodes would be an exercise
for
the reader.
--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2009-04-06 23:04
(Received via mailing list)
> Well that's part of it. But, if by "generating bytecode from another
> language" means, for instance, executing JVM bytecodes, I don't think it
> will fly. The term bytecode here is being used rather generically. The YARV
> "bytecodes" aren't the same as the JVM bytecodes, any more than
> Smalltalk-80
> bytecodes are the same as either..
>
> Getting another language to produce YARV bytecodes would be an exercise for
> the reader.


I meant that I'd like to try and generate YARV bytecode from non-Ruby
source. Mostly a pie-in-the-sky idea, only just writing my first
AST-walking
interpreter, just wondering if it's possible.
770048af205ed307b8cf35ae2282ee2f?d=identicon&s=25 Michael Malone (Guest)
on 2009-04-06 23:48
(Received via mailing list)
James Coglan wrote:
>
>
> I meant that I'd like to try and generate YARV bytecode from non-Ruby
> source. Mostly a pie-in-the-sky idea, only just writing my first AST-walking
> interpreter, just wondering if it's possible.
>
>
It's definitely possible.  I suggest you take a look at the parrot
interpreter.  It is a one-for-all interpreter (well, that's the aim,
anyway), which began life as an April Fool's joke that someone took one
step further.  It officially hit version 1.0 the other day, so it should
be a good example for you.  By one-for-all I mean, it's a generic
interpreter that can handle many languages, much like .NET and the clr,
I guess.

=======================================================================
This email, including any attachments, is only for the intended
addressee.  It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
 altered or corrupted during transmission.
=======================================================================
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-04-07 11:05
(Received via mailing list)
On Mon, Apr 6, 2009 at 11:03 PM, James Coglan <jcoglan@googlemail.com>
wrote:
<snip>
>
> I meant that I'd like to try and generate YARV bytecode from non-Ruby
> source. Mostly a pie-in-the-sky idea, only just writing my first AST-walking
> interpreter, just wondering if it's possible.
Having YARV/Rubinius as a backend for other languages is certainly a
very reasonable idea.
And if I understood correctly it is planned to be possible in the
(near?) future.

Cheers
R.
49d6f762505615af259534deed94bc54?d=identicon&s=25 Daniel Bovensiepen (Guest)
on 2009-04-07 23:02
(Received via mailing list)
Hi James,

> I just found out about the VM::InstructionSequence.compile method for
> turning Ruby code strings into ruby1.9 bytecode. Is there a standard method
> of de/serializing this bytecode to/from files, and is there a way of running
> bytecode directly? I'm looking at generating bytecode from another language
> to get it to run on Ruby.
>

You can at least load bytecode directly, transform ruby to bytecode and
at
the end evaluate everything:

1
2
3
4
5
6
7
8
9
*10*
11
12
13
14
15
16
17
18
19
*20*
iseqFromCode = RubyVM::InstructionSequence.new "a = 1; a += a; puts a"
  # => <RubyVM::InstructionSequence:<compiled>@<compiled>>
iseqFromCode.eval
  # 2
  # => nil
iseqRawArr = iseqFromCode.to_a
  # => ["YARVInstructionSequence/SimpleDataFormat", 1, 1, 1,
  #     {:arg_size=>0, :local_size=>2, :stack_max=>2}, "<compiled>",
  #     "<compiled>", :top, [:a], 0, [], [1, [:trace, 1],
  #     [:putobject, 1], [:setlocal, 2], [:trace, 1], [:getlocal, 2],
  #     [:getlocal, 2], [:opt_plus], [:setlocal, 2], [:trace, 1],
  #     [:putnil], [:getlocal, 2], [:send, :puts, 1, nil, 8, nil],
  #     [:leave]]]
iseqRawArr.class
  # => Array
iseqFromArr = RubyVM::InstructionSequence.load iseqRawArr
  # => <RubyVM::InstructionSequence:<compiled>@<compiled>>
iseqFromArr.eval
  # 2
  # => nil

But for this you have to apply this to the source code:

1
2
3
4
5
6
7
8
9
*10*
11
12
13
14
15
boviAir:src danielbovensiepen$ svn diff iseq.c
Index: iseq.c
===================================================================
--- iseq.c      (revision 21348)
+++ iseq.c      (working copy)
@@ -1451,7 +1451,7 @@
     rb_define_method(rb_cISeq, "eval", iseq_eval, 0);

     /* disable this feature because there is no verifier. */
-    /* rb_define_singleton_method(rb_cISeq, "load", iseq_s_load, -1);
*/
+    rb_define_singleton_method(rb_cISeq, "load", iseq_s_load, -1);
     (void)iseq_s_load;

     rb_define_singleton_method(rb_cISeq, "compile", iseq_s_compile,
-1);
boviAir:src danielbovensiepen$

Of course, the comments are very clear: you shouldn't do this!


MfG
Daniel
This topic is locked and can not be replied to.