what does llvmruby look like?
LLVMRuby is a pretty straighforward wrapper around the C++ API for generating LLVM bytecode and JIT compilation. The LLVM API is both very well designed and very object oriented and so maps pretty straightforwardly into Ruby. Here is a simple example of using LLVMRuby to construct and JIT compile a function which manipulates native Ruby objects.
require 'llvm' include LLVM include RubyInternals class Builder include RubyHelpers end m = LLVM::Module.new('ruby_bindings_examples') ExecutionEngine.get(m) def ftype(ret_type, arg_types) Type.function(ret_type, arg_types) end rb_ary_new = m.external_function('rb_ary_new', ftype(VALUE, [])) rb_to_id = m.external_function('rb_to_id', ftype(VALUE, [VALUE])) rb_ivar_get = m.external_function('rb_ivar_get', ftype(VALUE, [VALUE, ID])) rb_ivar_set = m.external_function('rb_ivar_set', ftype(VALUE, [VALUE, ID, VALUE])) class TestClass def initialize @shaka = 'khan' end end test_instance = TestClass.new # take an object and an instance variable symbol, return value of instance variable type = Type.function(VALUE, [VALUE, VALUE]) f = m.get_or_insert_function('shakula', type) obj, ivar_sym = f.arguments b = f.create_block.builder new_ary = b.call(rb_ary_new) ivar_id = b.call(rb_to_id, ivar_sym) ret_val = b.call(rb_ivar_get, obj, ivar_id) b.return(ret_val) ret = ExecutionEngine.run_function(f, test_instance, :@shaka) puts "get instance variable @shaka: #{ret.inspect}"
??, ??????? ???????…
??????? ???????? Here is a simple example of using LLVMRuby to construct and JIT compile a function which manipulates […….
Kylie Batt
19 May 10 at 8:07 pm