ruby-libjit 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,5 +1,7 @@
1
- Ruby-libjit 0.2.0
2
- Copyright (C) 2008 Paul Brannan
1
+ Ruby-libjit
2
+ :include: VERSION
3
+
4
+ Copyright (C) 2009 Paul Brannan
3
5
 
4
6
  Ruby-libjit is a wrapper for the libjit library. It provides basic
5
7
  functionality for jit-compiling functions, including integrating those
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.1
data/ext/jit.c CHANGED
@@ -182,6 +182,22 @@ static VALUE context_build(VALUE self)
182
182
  #endif
183
183
  }
184
184
 
185
+ static VALUE function_s_compile(int argc, VALUE * argv, VALUE klass);
186
+
187
+ /*
188
+ * call-seq:
189
+ * function = context.compile_function(signature) { |f| ... }
190
+ *
191
+ * Acquire a lock on the context so it can be used to build a function.
192
+ */
193
+ static VALUE context_compile_function(VALUE self, VALUE signature)
194
+ {
195
+ /* TODO: support passing in parent function */
196
+ VALUE argv[] = { self, signature };
197
+ int argc = 2;
198
+ return function_s_compile(argc, argv, rb_cFunction);
199
+ }
200
+
185
201
  /*
186
202
  * call-seq:
187
203
  * Context.build { |context| ... }
@@ -259,6 +275,19 @@ static VALUE create_function(int argc, VALUE * argv, VALUE klass)
259
275
 
260
276
  rb_scan_args(argc, argv, "21", &context_v, &signature_v, &parent_function_v);
261
277
 
278
+ /* Allow the user to specific the signature as a hash */
279
+ if (TYPE(signature_v) == T_HASH)
280
+ {
281
+ signature_v = rb_funcall(
282
+ rb_cType,
283
+ rb_intern("create_signature"),
284
+ 1,
285
+ signature_v);
286
+ }
287
+
288
+ check_type("context", rb_cContext, context_v);
289
+ check_type("signature", rb_cType, signature_v);
290
+
262
291
  Data_Get_Struct(context_v, struct _jit_context, context);
263
292
  Data_Get_Struct(signature_v, struct _jit_type, signature);
264
293
 
@@ -1008,7 +1037,7 @@ static VALUE wrap_type(jit_type_t type)
1008
1037
 
1009
1038
  /*
1010
1039
  * call-seq:
1011
- * type = Type.create_signature(abi, return_type, array_of_param_types)
1040
+ * type = Type._create_signature(abi, return_type, array_of_param_types)
1012
1041
  *
1013
1042
  * Create a new signature.
1014
1043
  */
@@ -1436,6 +1465,7 @@ void Init_jit()
1436
1465
  rb_cContext = rb_define_class_under(rb_mJIT, "Context", rb_cObject);
1437
1466
  rb_define_singleton_method(rb_cContext, "new", context_s_new, 0);
1438
1467
  rb_define_method(rb_cContext, "build", context_build, 0);
1468
+ rb_define_method(rb_cContext, "compile_function", context_compile_function, 1);
1439
1469
  rb_define_singleton_method(rb_cContext, "build", context_s_build, 0);
1440
1470
 
1441
1471
  rb_cClosure = rb_define_class_under(rb_mJIT, "Closure", rb_cObject);
@@ -1465,7 +1495,7 @@ void Init_jit()
1465
1495
  rb_define_method(rb_cFunction, "compiled?", function_is_compiled, 0);
1466
1496
 
1467
1497
  rb_cType = rb_define_class_under(rb_mJIT, "Type", rb_cObject);
1468
- rb_define_singleton_method(rb_cType, "create_signature", type_s_create_signature, 3);
1498
+ rb_define_singleton_method(rb_cType, "_create_signature", type_s_create_signature, 3);
1469
1499
  rb_define_singleton_method(rb_cType, "create_struct", type_s_create_struct, 1);
1470
1500
  rb_define_singleton_method(rb_cType, "create_pointer", type_s_create_pointer, 1);
1471
1501
  rb_define_method(rb_cType, "get_offset", type_get_offset, 1);
data/lib/jit.rb CHANGED
@@ -3,3 +3,4 @@ require 'jit/array'
3
3
  require 'jit/function'
4
4
  require 'jit/struct'
5
5
  require 'jit/value'
6
+ require 'jit/type'
data/lib/jit/type.rb ADDED
@@ -0,0 +1,38 @@
1
+ module JIT
2
+ class Type
3
+ # Create a new signature.
4
+ #
5
+ # call-seq:
6
+ # jit = JIT::Type.create_signature(abi, return_type, array_of_param_types)
7
+ # jit = JIT::Type.create_signature(array_of_param_types => return_type)
8
+ # jit = JIT::Type.create_signature(array_of_param_types => return_type, :abi => abi)
9
+ def self.create_signature(*args)
10
+ if args.size == 1 then
11
+ h = args[0]
12
+ return_type = nil
13
+ param_types = nil
14
+ abi = :CDECL
15
+ h.each do |k, v|
16
+ if k == :abi then
17
+ abi = v
18
+ else
19
+ param_types = k
20
+ return_type = v
21
+ end
22
+ end
23
+
24
+ if return_type.nil? or param_types.nil? then
25
+ raise ArgumentError, "Missing return_type and/or param types"
26
+ end
27
+
28
+ return self._create_signature(abi, return_type, param_types)
29
+
30
+ elsif args.size == 3 then
31
+ return self._create_signature(args[0], args[1], args[2])
32
+
33
+ else
34
+ raise ArgumentError, "Wrong number of arguments (expected 3)"
35
+ end
36
+ end
37
+ end
38
+ end
data/sample/fib.rb CHANGED
@@ -1,11 +1,6 @@
1
1
  require 'jit'
2
2
 
3
- fib = nil
4
- signature = JIT::Type.create_signature(
5
- :CDECL,
6
- :INT,
7
- [ :INT ])
8
- fib = JIT::Function.build(signature) do |f|
3
+ fib = JIT::Function.build([:INT] => :INT) do |f|
9
4
  n = f.param(0)
10
5
 
11
6
  a = f.value(:INT, 0)
data/sample/simple.rb CHANGED
@@ -1,15 +1,9 @@
1
1
  require 'jit'
2
2
 
3
- function = nil
4
- JIT::Context.build do |context|
5
- signature = JIT::Type.create_signature(
6
- JIT::ABI::CDECL,
7
- JIT::Type::INT, # returns an integer
8
- [ JIT::Type::INT ]) # and tages an integer as a parameter
9
- function = JIT::Function.compile(context, signature) do |f|
10
- value = f.get_param(0)
11
- f.insn_return(value)
12
- end
3
+ multiply = JIT::Function.build([:INT, :INT] => :INT) do |f|
4
+ lhs = f.param(0)
5
+ rhs = f.param(1)
6
+ f.return(lhs * rhs)
13
7
  end
14
8
 
15
- p function.apply(42) #=> 42
9
+ p multiply.apply(6, 7) #=> 42
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-libjit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Brannan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-02 00:00:00 -05:00
12
+ date: 2009-12-04 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -27,6 +27,7 @@ extensions:
27
27
  extra_rdoc_files: []
28
28
 
29
29
  files:
30
+ - VERSION
30
31
  - COPYING
31
32
  - LGPL
32
33
  - LICENSE
@@ -37,6 +38,7 @@ files:
37
38
  - lib/jit/struct.rb
38
39
  - lib/jit/pointer.rb
39
40
  - lib/jit/value.rb
41
+ - lib/jit/type.rb
40
42
  - ext/extconf.rb
41
43
  - ext/rubypp.rb
42
44
  - ext/jit.c