cranelift_ruby 0.1.0 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1487b62468b109e4245cee90074f2372c2cb7abeecf33275bdce071fef1e220
4
- data.tar.gz: 91d1a096961545faf0ba4da70a19a58b1a395c4b0419303ce6abafbd8ee49a80
3
+ metadata.gz: '039ede96c7d734182f363ea730befc04ae5cd9dd13e2564219e6dd03beae4d64'
4
+ data.tar.gz: 164e40e024c40bf14df62dad63189a4d428eed20ffcfb13f3c584dc91eb8d586
5
5
  SHA512:
6
- metadata.gz: b56586435590db7e090b14b372bceeb32fd9c41955a4528dea99d6ce77413278d150b4c80e681a843c50d29cb7573e91f199ed2323f4611b5c92d13fc173b669
7
- data.tar.gz: ce719fe18e79a14de52bab14745ceeae9cda0cadf9f9ac597cc8792dc729a7c059cc3039d93f7b92fc831abcf55b9406f2dbad2eabadfd789d059d28a7ec459c
6
+ metadata.gz: 81228c2a238389ffa49236b29cb8f3017afbd90f47237d2a03cf966c67b6657ecd94538f0ce0000779d0779b1d1ca995fd4120f4628641fdb54f542e94ab2f13
7
+ data.tar.gz: f57c3c484724faf31e3738311cdfaa3bcf3f2b3dcdf6f8bed1a77e53724b6040689fd12c615325da440cb74cfbfc9ad1746c9828fb17150058811f9efcb340ff
data/Rakefile CHANGED
@@ -34,4 +34,4 @@ Rake::TestTask.new(test_clean: %i[tidy_up bundle_install build_lib]) do |t|
34
34
  t.test_files = FileList["test/**/*_test.rb"]
35
35
  end
36
36
 
37
- task default: :test_clean
37
+ task default: :build_lib
@@ -1,3 +1,3 @@
1
1
  module CraneliftRuby
2
- VERSION = "0.1.0"
3
- end
2
+ VERSION = "0.1.4"
3
+ end
data/src/lib.rs CHANGED
@@ -5,6 +5,7 @@ extern crate rutie;
5
5
  extern crate lazy_static;
6
6
 
7
7
  use std::convert::TryInto;
8
+ use std::mem;
8
9
  use std::num::TryFromIntError;
9
10
 
10
11
  use cranelift::codegen::binemit::{NullStackMapSink, NullTrapSink};
@@ -14,18 +15,19 @@ use cranelift::prelude::*;
14
15
  use cranelift_jit::{JITBuilder, JITModule};
15
16
  use cranelift_module::{default_libcall_names, FuncId, Linkage, Module};
16
17
  use eyre::{eyre, Result};
17
- use rutie::{
18
- AnyException, AnyObject, Array, Class, Exception, Integer, NilClass, Object, Proc, RString,
19
- Symbol, VM,
20
- };
18
+ use rutie::rubysys::class;
19
+ use rutie::types::Argc;
20
+ use rutie::util::str_to_cstring;
21
+ use rutie::{AnyException, AnyObject, Array, Boolean, Class, Exception, Integer, NilClass, Object, Proc, RString, Symbol, VM};
21
22
  class!(Cranelift);
22
23
 
23
24
  pub struct Builder {
24
25
  pub module: JITModule,
26
+ pub verbose: bool
25
27
  }
26
28
 
27
29
  impl Builder {
28
- fn new() -> Result<Self> {
30
+ fn new(verbose: bool) -> Result<Self> {
29
31
  let mut flag_builder = settings::builder();
30
32
  flag_builder.set("use_colocated_libcalls", "false")?;
31
33
  // FIXME set back to true once the x64 backend supports it.
@@ -35,6 +37,7 @@ impl Builder {
35
37
  let isa = isa_builder.finish(settings::Flags::new(flag_builder));
36
38
  Ok(Self {
37
39
  module: JITModule::new(JITBuilder::with_isa(isa, default_libcall_names())),
40
+ verbose: verbose
38
41
  })
39
42
  }
40
43
  }
@@ -43,6 +46,36 @@ wrappable_struct!(Builder, BuilderWrapper, BUILDER_WRAPPER);
43
46
 
44
47
  class!(CraneliftBuilder);
45
48
 
49
+ pub extern fn cranelift_builder_new(argc: Argc, argv: *const AnyObject, _rtself: AnyObject) -> AnyObject {
50
+ let args = rutie::types::Value::from(0);
51
+
52
+ unsafe {
53
+ let p_argv: *const rutie::types::Value = mem::transmute(argv);
54
+
55
+ class::rb_scan_args(
56
+ argc,
57
+ p_argv,
58
+ str_to_cstring("*").as_ptr(),
59
+ &args
60
+ )
61
+ };
62
+
63
+ let arguments = Array::from(args);
64
+ let verbose = {
65
+ if arguments.length() == 1 {
66
+ arguments.at(0).try_convert_to::<Boolean>().map_err(|e| VM::raise_ex(e)).unwrap().to_bool()
67
+ } else {
68
+ false
69
+ }
70
+ };
71
+ let builder = Builder::new(verbose)
72
+ .map_err(|e| VM::raise_ex(AnyException::new("StandardError", Some(&e.to_string()))))
73
+ .unwrap();
74
+ Class::from_existing("CraneliftRuby")
75
+ .get_nested_class("CraneliftBuilder")
76
+ .wrap_data(builder, &*BUILDER_WRAPPER)
77
+ }
78
+
46
79
  methods!(
47
80
  CraneliftBuilder,
48
81
  itself,
@@ -58,14 +91,6 @@ methods!(
58
91
  .get_nested_class("Signature")
59
92
  .wrap_data(sig, &*SIGNATURE_WRAPPER)
60
93
  },
61
- fn cranelift_builder_new() -> AnyObject {
62
- let builder = Builder::new()
63
- .map_err(|e| VM::raise_ex(AnyException::new("StandardError", Some(&e.to_string()))))
64
- .unwrap();
65
- Class::from_existing("CraneliftRuby")
66
- .get_nested_class("CraneliftBuilder")
67
- .wrap_data(builder, &*BUILDER_WRAPPER)
68
- },
69
94
  fn make_function(name: RString, signature: AnyObject, callback: Proc) -> Integer {
70
95
  let builder = itself.get_data_mut(&*BUILDER_WRAPPER);
71
96
  let name = name.map_err(|e| VM::raise_ex(e)).unwrap();
@@ -152,7 +177,7 @@ fn make_function_impl(
152
177
  let mut func_ctx = FunctionBuilderContext::new();
153
178
  ctx.func.signature = signature.clone();
154
179
  ctx.func.name = ExternalName::user(0, func.as_u32());
155
- // ctx.set_disasm(true);
180
+ ctx.set_disasm(builder.verbose);
156
181
  let bcx: FunctionBuilder = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
157
182
  let cranelift_bcx: AnyObject = Class::from_existing("CraneliftRuby")
158
183
  .get_nested_class("CraneliftFunctionBuilder")
@@ -160,12 +185,14 @@ fn make_function_impl(
160
185
  callback.call(&[cranelift_bcx]);
161
186
  let mut trap_sink = NullTrapSink {};
162
187
  let mut stack_map_sink = NullStackMapSink {};
163
- // println!("{}", ctx.func.display(None).to_string());
164
188
  builder
165
189
  .module
166
190
  .define_function(func, &mut ctx, &mut trap_sink, &mut stack_map_sink)
167
191
  .map_err(|e| AnyException::new("StandardError", Some(&format!("{:?}", e))))?;
168
- // println!("code: {}", ctx.mach_compile_result.unwrap().disasm.unwrap());
192
+ if builder.verbose {
193
+ println!("\n{}", ctx.func.display(None).to_string());
194
+ println!("{}\n", ctx.mach_compile_result.unwrap().disasm.unwrap());
195
+ }
169
196
  Ok(func.as_u32())
170
197
  }
171
198
 
@@ -343,6 +370,34 @@ methods!(
343
370
  let res = bcx.ins().iadd(x, y);
344
371
  Integer::new(res.as_u32().into())
345
372
  },
373
+ fn band(x: Integer, y: Integer) -> Integer {
374
+ let bcx = itself.get_data_mut(&*FUNCTION_BUILDER_WRAPPER);
375
+ let x = x.map_err(|e| VM::raise_ex(e)).unwrap();
376
+ let x = from_integer_to_value(x)
377
+ .map_err(|e| VM::raise_ex(e))
378
+ .unwrap();
379
+ let y = y.map_err(|e| VM::raise_ex(e)).unwrap();
380
+ let y = from_integer_to_value(y)
381
+ .map_err(|e| VM::raise_ex(e))
382
+ .unwrap();
383
+
384
+ let res = bcx.ins().band(x, y);
385
+ Integer::new(res.as_u32().into())
386
+ },
387
+ fn bor(x: Integer, y: Integer) -> Integer {
388
+ let bcx = itself.get_data_mut(&*FUNCTION_BUILDER_WRAPPER);
389
+ let x = x.map_err(|e| VM::raise_ex(e)).unwrap();
390
+ let x = from_integer_to_value(x)
391
+ .map_err(|e| VM::raise_ex(e))
392
+ .unwrap();
393
+ let y = y.map_err(|e| VM::raise_ex(e)).unwrap();
394
+ let y = from_integer_to_value(y)
395
+ .map_err(|e| VM::raise_ex(e))
396
+ .unwrap();
397
+
398
+ let res = bcx.ins().bor(x, y);
399
+ Integer::new(res.as_u32().into())
400
+ },
346
401
  fn return_(values: Array) -> NilClass {
347
402
  let bcx = itself.get_data_mut(&*FUNCTION_BUILDER_WRAPPER);
348
403
  let values = values.map_err(|e| VM::raise_ex(e)).unwrap();
@@ -591,9 +646,12 @@ pub extern "C" fn Init_cranelift_ruby() {
591
646
  klass.def("def_var", def_var);
592
647
  klass.def("use_var", use_var);
593
648
  klass.def("icmp", icmp);
649
+ klass.def("select", select);
594
650
  klass.def("import_signature", import_signature);
595
651
  klass.def("call_indirect", call_indirect);
596
652
  klass.def("inst_results", inst_results);
653
+ klass.def("band", band);
654
+ klass.def("bor", bor);
597
655
  });
598
656
  });
599
657
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cranelift_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Concetto Rudilosso
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-23 00:00:00.000000000 Z
11
+ date: 2021-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rutie