support for 32-bit and 64-bit ruby interpreters
Tonight, I implemented and tested support for 32-bit Ruby. How does this work?
// Figure out details of the target machine const IntegerType *machine_word_type; if(sizeof(void*) == 4) { machine_word_type = Type::Int32Ty; } else { machine_word_type = Type::Int64Ty; } rb_define_const(cLLVMRuby, "MACHINE_WORD", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(machine_word_type)));
First the extension creates a MACHINE_WORD type based on the pointer size of the machine.
# describe structures used by the ruby 1.8/1.9 interpreters module RubyInternals FIXNUM_FLAG = 0x1.llvm CHAR = Type::Int8Ty P_CHAR = Type::pointer(CHAR) LONG = MACHINE_WORD VALUE = MACHINE_WORD P_VALUE = Type::pointer(VALUE) ID = MACHINE_WORD RBASIC = Type::struct([VALUE, VALUE]) RARRAY = Type::struct([RBASIC, LONG, LONG, P_VALUE]) P_RARRAY = Type::pointer(RARRAY) RSTRING = Type::struct([RBASIC, LONG, P_CHAR, VALUE]) P_RSTRING = Type::pointer(RSTRING) end
All the Ruby data types are defined in the RubyInternals module.
If you are familiar with Ruby internals from having worked on C extensions, you will know that everything is a VALUE, which is one machine word that is either nil, true, false, a FixInt, or a pointer to a struct somewhere on the heap. This makes our definitions pretty simple, all that changes between machines is the size of this pointer.
You’ll also notice in this code, that the native Ruby data types themselves are defined in Ruby code here. For example, an Array in Ruby is represented internally using this struct:
struct RArray { struct RBasic basic; long len; union { long capa; VALUE shared; } aux; VALUE *ptr; };
And in our Ruby mappings that allows us to work on this structure, it’s defined basically the same way using LLVM data types:
RARRAY = Type::struct([RBASIC, LONG, LONG, P_VALUE])
Easy!
??? …
?? ??? ??????, ??? ?????????? ??????, ???? ????????? ??????? ? ??????????….
????? ????????
17 Mar 10 at 3:01 am
? ?????????, ??, ??-?????, ?? ?????????? ??????. ????????? ??? ????????. ?????? ??? ? PM, ??????????….
????????????? ? ?????????????? Tonight, I implemented and tested support for 32-bit Ruby…..
Kylie BattName
11 Apr 10 at 6:36 pm