Ruby2c 1.0.0 beta 2 released

We've released ruby2c 1.0.0 beta 2!

RubyToC has the following modules:

  Rewriter - massages the sexp into a more consistent form.
  TypeChecker - type inferencer for the above sexps.
  RubyToC - converts a ruby (subset) sexp to C.

and the following tools:

  translate.rb - Translates a given file to C.

Understand what we mean by beta. It means we need eyes on it, it means it was ready enough to put out in the wild, but it also means that it isn't ready for any real use.

  What can it do?

  Well, currently it can pass all of its unit tests (325 tests with 512 assertions) and it can translate nice simple static algorithmic code into C without much problem. For example:
  & cat x.rb
  class Something
  def blah; return 2+2; end
  def main; return blah; end
  end
  & ./translate.rb x.rb > x.c
  & gcc -I /usr/local/lib/ruby/1.8/powerpc-darwin x.c
  x.c: In function `main':
  x.c:17: warning: return type of `main' is not `int'
  & ./a.out
  & echo $?
  4

  What can it not do?

  More than it can.

  It can't (and won't) translate dynamic code. Period. That is simply not the intent.

  It probably can't translate a lot of static code that we simply haven't come across or anticipated yet. Our tests cover a fair amount, our validation runs cover a lot more than that, but it is still fairly idiomatic ruby and that puts us at being better at certain styles of coding and much worse at others.

  It is also simply rough around the edges. We've rounded out the rdoc but haven't done a thing for general documentation yet. These are on our list, and rather high on our priority list, but we just haven't had the time yet. For now, check out the rdoc and the PDF presentation that we've had up for a while.

  PLEASE: file bugs! We need feedback and we'd like to be able to track it. The ruby2c project is on rubyforge and I'm getting the trackers set up today as well.

CHANGES:

*** 1.0.0-beta2 / 2004-02-15

+ 1 minor enhancement
  + Added post-condition while/until support and tests.
+ 4 bug fixes
  + Fixed bug:1422: Escape newlines to prevent multi-line strings in C.
  + Fixed bug:1429: Arrays of strings are not being properly.
  + Fixed bug:1447/1448: Readme file's website and added ParseTree dependency.

···

--
ryand-ruby@zenspider.com - Seattle.rb - http://www.zenspider.com/seattle.rb
http://blog.zenspider.com/ - http://rubyforge.org/projects/ruby2c

I just want to ask if

puts "foo" + 2

will fail before gcc-time? Or do you use rtti so typing is still put
off till run-time?

···

On Wed, 16 Feb 2005 04:52:24 +0900, Ryan Davis <ryand-ruby@zenspider.com> wrote:

We've released ruby2c 1.0.0 beta 2!

RubyToC has the following modules:

        Rewriter - massages the sexp into a more consistent form.
        TypeChecker - type inferencer for the above sexps.
        RubyToC - converts a ruby (subset) sexp to C.

and the following tools:

        translate.rb - Translates a given file to C.

Understand what we mean by beta. It means we need eyes on it, it means
it was ready enough to put out in the wild, but it also means that it
isn't ready for any real use.

  What can it do?

  Well, currently it can pass all of its unit tests (325 tests with 512
assertions) and it can translate nice simple static algorithmic code
into C without much problem. For example:
  & cat x.rb
  class Something
  def blah; return 2+2; end
  def main; return blah; end
  end
  & ./translate.rb x.rb > x.c
  & gcc -I /usr/local/lib/ruby/1.8/powerpc-darwin x.c
  x.c: In function `main':
  x.c:17: warning: return type of `main' is not `int'
  & ./a.out
  & echo $?
  4

  What can it not do?

  More than it can.

  It can't (and won't) translate dynamic code. Period. That is simply
not the intent.

  It probably can't translate a lot of static code that we simply
haven't come across or anticipated yet. Our tests cover a fair amount,
our validation runs cover a lot more than that, but it is still fairly
idiomatic ruby and that puts us at being better at certain styles of
coding and much worse at others.

  It is also simply rough around the edges. We've rounded out the rdoc
but haven't done a thing for general documentation yet. These are on
our list, and rather high on our priority list, but we just haven't had
the time yet. For now, check out the rdoc and the PDF presentation that
we've had up for a while.

  PLEASE: file bugs! We need feedback and we'd like to be able to track
it. The ruby2c project is on rubyforge and I'm getting the trackers set
up today as well.

CHANGES:

*** 1.0.0-beta2 / 2004-02-15

+ 1 minor enhancement
        + Added post-condition while/until support and tests.
+ 4 bug fixes
        + Fixed bug:1422: Escape newlines to prevent multi-line strings in C.
        + Fixed bug:1429: Arrays of strings are not being properly.
        + Fixed bug:1447/1448: Readme file's website and added ParseTree
dependency.

--
ryand-ruby@zenspider.com - Seattle.rb -
Seattle.rb | Home
http://blog.zenspider.com/ - http://rubyforge.org/projects/ruby2c

--
spooq

Checking out the writeup on Ruby2C at:

http://www.zenspider.com/~ryand/Ruby2C.pdf

there is one thing about the example on page 25 (of the pdf)
which concerns me a little bit. The example has:

class MyTest
  def factorial(n)
    f = 1
    n.downto(2) { |x| f *= x }
    return f
  end
end

turn into:

static VALUE factorial(VALUE self,
                        VALUE _n) {
    long n = NUM2INT(_n);
    long f;
    long x;
    f = 1;
    x = n;
    while (x >= 2) {
        f = f * x;
        x = x - 1;
    };
    return INT2NUM(f);
}

One nice thing about ruby is that the programmer does not have
to worry about supported-ranges for integer values. Ruby just
switches from "fixnum" objects into "bignum" objects, without
any attention from the programmer. It seems to me that the C
code that is listed above just assumes the multiplication of
'f * x' will not overflow. Is that the case?

···

--
Garance Alistair Drosehn = drosihn@gmail.com

Luke Graham wrote:

I just want to ask if

puts "foo" + 2

will fail before gcc-time? Or do you use rtti so typing is still put
off till run-time?

I'm not sure it *could* be smart enough to catch that.
There could be a Integer#to_str defined somewhere, perhaps
at runtime.

Hal

It _should_ fail, but currently doesn't. We don't do RTTI, we do type inference at translation time, before the C emit phase, so this should be caught. I'm filing a bug now.

···

On Feb 15, 2005, at 4:26 PM, Luke Graham wrote:

I just want to ask if

puts "foo" + 2

will fail before gcc-time? Or do you use rtti so typing is still put
off till run-time?

--
ryand-ruby@zenspider.com - Seattle.rb - Seattle.rb | Home
http://blog.zenspider.com/ - http://rubyforge.org/projects/ruby2c

You are correct.

We could add the ability to automatically add the bignum/fixnum auto-conversion stuff, but we've not needed the capability yet.

···

On 04 Jul 2005, at 15:22, Garance A Drosehn wrote:

Checking out the writeup on Ruby2C at:

http://www.zenspider.com/~ryand/Ruby2C.pdf

there is one thing about the example on page 25 (of the pdf)
which concerns me a little bit. The example has:

class MyTest
  def factorial(n)
    f = 1
    n.downto(2) { |x| f *= x }
    return f
  end
end

turn into:

static VALUE factorial(VALUE self,
                        VALUE _n) {
    long n = NUM2INT(_n);
    long f;
    long x;
    f = 1;
    x = n;
    while (x >= 2) {
        f = f * x;
        x = x - 1;
    };
    return INT2NUM(f);
}

One nice thing about ruby is that the programmer does not have
to worry about supported-ranges for integer values. Ruby just
switches from "fixnum" objects into "bignum" objects, without
any attention from the programmer. It seems to me that the C
code that is listed above just assumes the multiplication of
'f * x' will not overflow. Is that the case?

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

I'm afraid that in your "concern" you missed the whole point of that demonstration, which is:

                 OOOOO SHINY!

···

On Jul 4, 2005, at 3:22 PM, Garance A Drosehn wrote:

there is one thing about the example on page 25 (of the pdf)
which concerns me a little bit. The example has:

class MyTest
  def factorial(n)
    f = 1
    n.downto(2) { |x| f *= x }
    return f
  end
end

turn into:

static VALUE factorial(VALUE self,
                        VALUE _n) {
    long n = NUM2INT(_n);
    long f;
    long x;
    f = 1;
    x = n;
    while (x >= 2) {
        f = f * x;
        x = x - 1;
    };
    return INT2NUM(f);
}

[cut]

--
ryand-ruby@zenspider.com - Seattle.rb - Seattle.rb | Home
http://blog.zenspider.com/ - http://rubyforge.org/projects/ruby2c

Luke Graham wrote:

I just want to ask if puts "foo" + 2
will fail before gcc-time? Or do you use rtti so typing is still put
off till run-time?

I'm not sure it *could* be smart enough to catch that.

Sure it can!

There could be a Integer#to_str defined somewhere, perhaps
at runtime.

Remember, ruby2c is all about static conversion, not dynamic. I suggest you read our propaganda at:

  http://www.zenspider.com/~ryand/Ruby2C.pdf

···

On Feb 15, 2005, at 4:37 PM, Hal Fulton wrote:

--
ryand-ruby@zenspider.com - Seattle.rb - Seattle.rb | Home
http://blog.zenspider.com/ - http://rubyforge.org/projects/ruby2c

It would be nice to at least detect that an overflow occurred, even
if you don't do the auto-conversion stuff. I do use ruby in some
situations simply because I don't want to have to think if I'm going
to be overflowing any fields that I'm collecting statistics into.

I do think the Ruby2C project could be quite useful, but I wouldn't
want to lose some of the more attractive characteristics of ruby
when taking advantage of it! :slight_smile:

···

On 7/5/05, Eric Hodel <drbrain@segment7.net> wrote:

On 04 Jul 2005, at 15:22, Garance A Drosehn wrote:
>
> One nice thing about ruby is that the programmer does not have
> to worry about supported-ranges for integer values. Ruby just
> switches from "fixnum" objects into "bignum" objects, without
> any attention from the programmer. It seems to me that the C
> code that is listed above just assumes the multiplication of
> 'f * x' will not overflow. Is that the case?

You are correct.

We could add the ability to automatically add the bignum/fixnum
auto-conversion stuff, but we've not needed the capability yet.

--
Garance Alistair Drosehn = drosihn@gmail.com
Senior Systems Programmer or gad@FreeBSD.org
Rensselaer Polytechnic Institute; Troy, NY; USA