fastruby 0.0.19 → 0.0.20

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. data/CHANGELOG +8 -0
  2. data/{README → README.rdoc} +6 -1
  3. data/Rakefile +7 -7
  4. data/benchmarks/benchmark.rb~ +14 -2
  5. data/ext/fastruby_base/fastruby_base.inl +8 -4
  6. data/lib/fastruby/builder/inference_updater.rb +76 -0
  7. data/lib/fastruby/builder/inference_updater.rb~ +76 -0
  8. data/lib/fastruby/builder/inferencer.rb +38 -0
  9. data/lib/fastruby/{inliner → builder}/inliner.rb +16 -27
  10. data/lib/fastruby/builder/inliner.rb~ +60 -0
  11. data/lib/fastruby/builder/locals_inference.rb +53 -0
  12. data/lib/fastruby/builder/lvar_type.rb +43 -0
  13. data/lib/fastruby/builder/lvar_type.rb~ +44 -0
  14. data/lib/fastruby/builder/pipeline.rb +43 -0
  15. data/lib/fastruby/builder/pipeline.rb~ +43 -0
  16. data/lib/fastruby/{reductor → builder}/reductor.rb +6 -3
  17. data/lib/fastruby/builder/reductor.rb~ +42 -0
  18. data/lib/fastruby/builder.rb +73 -25
  19. data/lib/fastruby/builder.rb~ +311 -0
  20. data/lib/fastruby/corelib/fixnum.rb +75 -0
  21. data/lib/fastruby/corelib/fixnum.rb~ +146 -0
  22. data/lib/fastruby/corelib/integer.rb +96 -0
  23. data/lib/fastruby/corelib/integer.rb~ +96 -0
  24. data/lib/fastruby/corelib.rb +23 -0
  25. data/lib/fastruby/corelib.rb~ +23 -0
  26. data/lib/fastruby/getlocals.rb +3 -1
  27. data/lib/fastruby/logging.rb +2 -2
  28. data/lib/fastruby/modules/inferencer/infer.rb +31 -0
  29. data/lib/fastruby/modules/inferencer/literal.rb +42 -0
  30. data/lib/fastruby/modules/inliner/call.rb +327 -0
  31. data/lib/fastruby/{inliner/modules/call.rb → modules/inliner/call.rb~} +14 -24
  32. data/lib/fastruby/modules/inliner/defn.rb +41 -0
  33. data/lib/fastruby/modules/inliner/defn.rb~ +29 -0
  34. data/lib/fastruby/modules/inliner/recursive.rb +40 -0
  35. data/lib/fastruby/{inliner/modules/recursive.rb → modules/inliner/recursive.rb~} +1 -1
  36. data/lib/fastruby/modules/lvar_type/call.rb +36 -0
  37. data/lib/fastruby/modules/lvar_type/call.rb~ +36 -0
  38. data/lib/fastruby/modules/lvar_type/defn.rb +42 -0
  39. data/lib/fastruby/modules/lvar_type/defn.rb~ +42 -0
  40. data/lib/fastruby/modules/lvar_type/lasgn.rb +41 -0
  41. data/lib/fastruby/modules/lvar_type/lasgn.rb~ +42 -0
  42. data/lib/fastruby/modules/lvar_type/recursive.rb +33 -0
  43. data/lib/fastruby/modules/lvar_type/recursive.rb~ +33 -0
  44. data/lib/fastruby/{reductor/modules → modules/reductor}/case.rb +0 -0
  45. data/lib/fastruby/modules/reductor/fastruby_flag.rb +33 -0
  46. data/lib/fastruby/{reductor/modules → modules/reductor}/for.rb +0 -0
  47. data/lib/fastruby/{reductor/modules → modules/reductor}/nontree.rb +0 -0
  48. data/lib/fastruby/modules/reductor/nontree.rb~ +32 -0
  49. data/lib/fastruby/{reductor/modules → modules/reductor}/recursive.rb +1 -1
  50. data/lib/fastruby/modules/reductor/recursive.rb~ +31 -0
  51. data/lib/fastruby/{translator/modules → modules/translator}/block.rb +0 -0
  52. data/lib/fastruby/modules/translator/call.rb +344 -0
  53. data/lib/fastruby/{translator/modules/call.rb → modules/translator/call.rb~} +24 -3
  54. data/lib/fastruby/{translator/modules → modules/translator}/defn.rb +10 -9
  55. data/lib/fastruby/modules/translator/defn.rb~ +267 -0
  56. data/lib/fastruby/{translator/modules → modules/translator}/directive.rb +3 -1
  57. data/lib/fastruby/modules/translator/directive.rb~ +44 -0
  58. data/lib/fastruby/{translator/modules → modules/translator}/exceptions.rb +3 -1
  59. data/lib/fastruby/modules/translator/exceptions.rb~ +120 -0
  60. data/lib/fastruby/{translator/modules → modules/translator}/flow.rb +0 -0
  61. data/lib/fastruby/modules/translator/iter.rb +745 -0
  62. data/lib/fastruby/{translator/modules/iter.rb → modules/translator/iter.rb~} +103 -48
  63. data/lib/fastruby/modules/translator/literal.rb +150 -0
  64. data/lib/fastruby/{translator/modules/literal.rb → modules/translator/literal.rb~} +3 -3
  65. data/lib/fastruby/{translator/modules → modules/translator}/logical.rb +0 -0
  66. data/lib/fastruby/{translator/modules → modules/translator}/method_group.rb +0 -0
  67. data/lib/fastruby/{translator/modules → modules/translator}/nonlocal.rb +18 -6
  68. data/lib/fastruby/modules/translator/nonlocal.rb~ +298 -0
  69. data/lib/fastruby/modules/translator/static.rb +290 -0
  70. data/lib/fastruby/{translator/modules/static.rb → modules/translator/static.rb~} +66 -17
  71. data/lib/fastruby/modules/translator/variable.rb +280 -0
  72. data/lib/fastruby/{translator/modules/variable.rb → modules/translator/variable.rb~} +14 -44
  73. data/lib/fastruby/modules.rb +30 -0
  74. data/lib/fastruby/object.rb +42 -6
  75. data/lib/fastruby/object.rb~ +159 -0
  76. data/lib/fastruby/set_tree.rb +7 -11
  77. data/lib/fastruby/set_tree.rb~ +71 -0
  78. data/lib/fastruby/sexp_extension.rb +29 -7
  79. data/lib/fastruby/sexp_extension.rb~ +262 -0
  80. data/lib/fastruby/translator/scope_mode_helper.rb~ +138 -0
  81. data/lib/fastruby/translator/translator.rb +87 -92
  82. data/lib/fastruby/translator/translator.rb~ +1600 -0
  83. data/lib/fastruby/translator/translator_modules.rb +3 -1
  84. data/lib/fastruby/translator/translator_modules.rb~ +53 -0
  85. data/lib/fastruby.rb +3 -1
  86. data/lib/fastruby.rb~ +3 -1
  87. data/lib/fastruby_only/base.rb +1 -0
  88. data/spec/corelib/numeric/fixnum_spec.rb +110 -0
  89. data/spec/corelib/numeric/fixnum_spec.rb~ +104 -0
  90. data/spec/corelib/numeric/integer_spec.rb +173 -0
  91. data/spec/corelib/numeric/integer_spec.rb~ +173 -0
  92. data/spec/fastruby_only/base_spec.rb +74 -0
  93. data/spec/graph/base_spec.rb +2 -1
  94. data/spec/graph/base_spec.rb~ +35 -0
  95. data/spec/graph/path_spec.rb +2 -2
  96. data/spec/graph/path_spec.rb~ +48 -0
  97. data/spec/graph/vertex_spec.rb +2 -1
  98. data/spec/graph/vertex_spec.rb~ +58 -0
  99. data/spec/reductor/base_spec.rb +1 -1
  100. data/spec/ruby/block/lambda_spec.rb~ +163 -0
  101. data/spec/ruby/block/proc_as_block_spec.rb~ +69 -1
  102. data/spec/ruby/block_spec.rb~ +2 -494
  103. data/spec/ruby/call/base_call_spec.rb +1 -1
  104. data/spec/ruby/call/base_call_spec.rb~ +2 -60
  105. data/spec/ruby/defn/replacement_spec.rb +26 -14
  106. data/spec/ruby/defn/replacement_spec.rb~ +13 -3
  107. data/spec/ruby/exception/internal_ex_spec.rb~ +86 -0
  108. data/spec/ruby/integrity_spec.rb~ +35 -1
  109. data/spec/ruby/variable_spec.rb~ +31 -0
  110. data/spec/scope_mode/flow_spec.rb +1 -1
  111. data/spec/scope_mode/flow_spec.rb~ +109 -0
  112. data/spec/sugar/base_spec.rb +29 -0
  113. data/spec/sugar/base_spec.rb~ +16 -0
  114. metadata +100 -43
  115. data/spec/fastruby/inliner/modules/call_spec.rb +0 -0
  116. data/spec/fastruby/translator/modules/nonlocal_spec.rb +0 -0
  117. data/spec/fastruby/translator/translator_spec.rb +0 -0
  118. data/spec/ruby/block/arguments_spec.rb~ +0 -214
  119. data/spec/ruby/block/break_spec.rb~ +0 -236
  120. data/spec/ruby/block/next_spec.rb~ +0 -85
  121. data/spec/ruby/block/retry_spec.rb~ +0 -43
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ 0.0.20 Fastruby implementation of some fixnum methods allowing inline it
2
+
3
+ Improve and bug fixing on inline
4
+
5
+ Syntantic sugar for fastruby method definitions (see README)
6
+
7
+ Internal design improvements, builder pipeline and translator decoupling
8
+
1
9
  0.0.19 Fixed temporal variable names of case blocks to make it deterministic and allow caching
2
10
 
3
11
  Fixed bug of duplicated entries on $LOAD_PATH, significant performance improvement saves 80% of time both with caching and without caching
@@ -33,6 +33,12 @@ direct calls instead of normal and expensive ruby calls.
33
33
 
34
34
  The currently implementation only can infer types for method and block arguments, and for literals
35
35
 
36
+ === Method inlining
37
+
38
+ Type information is used to inline methods by replacing calls with the implementation of method being called. This greatly increase the performance WITHOUT
39
+ breaking the expected dynamics of ruby code (e.g. allow replace an inlined method), many of corelib methods was reimplemented with ruby to allow inline them
40
+ and increase the performance of code using corelib methods (e.g. Fixnum methods)
41
+
36
42
  === Customization through build directives and API
37
43
 
38
44
  To compensate for the described limitations, fastruby suport a few build directives to allow the programmer help the inference.
@@ -61,7 +67,6 @@ I will stabilize the API and document it for next releases. I promise
61
67
  * fastruby on ruby1.8 is faster than ruby1.8 on a few cases (see benchmarks) but is slower than ruby1.9
62
68
  * callcc is not supported, it works but using it may result in unexpected behaviour
63
69
  * calls with blocks to ruby or cruby methods are almost as slow as normal ruby (if the called method is defined by fastruby, the call is pretty fast)
64
- * replacement of methods using identical code strings will work, but it will generate duplicated objects on the cache each time te snippet run
65
70
 
66
71
  == Usage
67
72
 
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ require "rspec/core/rake_task"
7
7
 
8
8
  spec = Gem::Specification.new do |s|
9
9
  s.name = 'fastruby'
10
- s.version = '0.0.19'
10
+ s.version = '0.0.20'
11
11
  s.author = 'Dario Seminara'
12
12
  s.email = 'robertodarioseminara@gmail.com'
13
13
  s.platform = Gem::Platform::RUBY
@@ -15,15 +15,15 @@ spec = Gem::Specification.new do |s|
15
15
  s.homepage = "http://github.com/tario/fastruby"
16
16
  s.add_dependency "RubyInline", "= 3.11.0"
17
17
  s.add_dependency "ruby_parser", ">= 2.0.6"
18
- s.add_dependency "define_method_handler", ">= 0.0.5"
18
+ s.add_dependency "define_method_handler", ">= 0.0.6"
19
19
  s.add_dependency "method_source", ">= 0.6.7"
20
20
  s.add_dependency "ruby2ruby", ">= 1.3.1"
21
21
  s.has_rdoc = true
22
- s.extra_rdoc_files = [ 'README' ]
22
+ s.extra_rdoc_files = [ 'README.rdoc' ]
23
23
  s.extensions = FileList["ext/**/extconf.rb"].to_a
24
- # s.rdoc_options << '--main' << 'README'
24
+ # s.rdoc_options << '--main' << 'README.rdoc'
25
25
  s.files = Dir.glob("{benchmarks,examples,lib,spec}/**/*") + Dir.glob("ext/**/*.inl")+ Dir.glob("ext/**/*.c") + Dir.glob("ext/**/*.h") + Dir.glob("ext/**/extconf.rb") +
26
- [ 'LICENSE', 'AUTHORS', 'README', 'Rakefile', 'TODO', 'CHANGELOG' ]
26
+ [ 'LICENSE', 'AUTHORS', 'README.rdoc', 'Rakefile', 'TODO', 'CHANGELOG' ]
27
27
  end
28
28
 
29
29
  desc 'Run tests'
@@ -35,8 +35,8 @@ end
35
35
  desc 'Generate RDoc'
36
36
  Rake::RDocTask.new :rdoc do |rd|
37
37
  rd.rdoc_dir = 'doc'
38
- rd.rdoc_files.add 'lib', 'README'
39
- rd.main = 'README'
38
+ rd.rdoc_files.add 'lib', 'README.rdoc'
39
+ rd.main = 'README.rdoc'
40
40
  end
41
41
 
42
42
  desc 'Build Gem'
@@ -1,9 +1,21 @@
1
1
  require "rubygems"
2
2
  require "fastruby"
3
3
 
4
+ class Fixnum
5
+ fastruby(:fastruby_only => true) do
6
+ def +(b)
7
+ _static{INT2FIX(FIX2INT(self)+FIX2INT(b))}
8
+ end
9
+
10
+ def -(b)
11
+ _static{INT2FIX(FIX2INT(self)-FIX2INT(b))}
12
+ end
13
+ end
14
+ end
15
+
4
16
  class X
5
17
  def foo(a,b)
6
- a+b
18
+ return a+b
7
19
  end
8
20
  end
9
21
 
@@ -15,7 +27,7 @@ class Y
15
27
 
16
28
  ret = 0
17
29
  while i > 0
18
- ret = (i+i)
30
+ ret = x.foo(i,i)
19
31
  i = i - 1
20
32
  end
21
33
  return ret
@@ -143,6 +143,10 @@ struct METHOD {
143
143
  NODE *body;
144
144
  };
145
145
 
146
+
147
+ RUBY_EXTERN void* ruby_current_thread;
148
+
149
+
146
150
  static inline void stack_chunk_initialize(struct STACKCHUNK* sc) {
147
151
  // initialize pointers with zeros
148
152
  memset(sc->pages, 0, sizeof(sc->pages));
@@ -351,16 +355,16 @@ static void init_stack_chunk() {
351
355
  rb_define_singleton_method(rb_cStackChunk, "create", rb_stack_chunk_create,0);
352
356
  rb_define_method(rb_cStackChunk, "alloc", rb_stack_chunk_alloc,1);
353
357
  }
354
-
358
+ # define NUM2PTR(x) ((void*)(NUM2ULONG(x)))
355
359
  static VALUE clear_method_hash_addresses(VALUE klass,VALUE rb_method_hash) {
356
-
357
360
  if (rb_method_hash != Qnil) {
358
361
  VALUE rb_values = rb_funcall(rb_method_hash, rb_intern("values"),0);
359
362
  void** address;
360
363
  int i;
364
+
361
365
  for (i = 0; i < _RARRAY_LEN(rb_values); i++) {
362
- address = (void**)FIX2LONG(rb_ary_entry(rb_values,i));
363
- *address = 0;
366
+ address = (void**)NUM2PTR(rb_ary_entry(rb_values,i));
367
+ *address = 0;
364
368
  }
365
369
  }
366
370
 
@@ -0,0 +1,76 @@
1
+ =begin
2
+
3
+ This file is part of the fastruby project, http://github.com/tario/fastruby
4
+
5
+ Copyright (c) 2011 Roberto Dario Seminara <robertodarioseminara@gmail.com>
6
+
7
+ fastruby is free software: you can redistribute it and/or modify
8
+ it under the terms of the gnu general public license as published by
9
+ the free software foundation, either version 3 of the license, or
10
+ (at your option) any later version.
11
+
12
+ fastruby is distributed in the hope that it will be useful,
13
+ but without any warranty; without even the implied warranty of
14
+ merchantability or fitness for a particular purpose. see the
15
+ gnu general public license for more details.
16
+
17
+ you should have received a copy of the gnu general public license
18
+ along with fastruby. if not, see <http://www.gnu.org/licenses/>.
19
+
20
+ =end
21
+ module FastRuby
22
+ class InferenceUpdater
23
+ class AddInfer
24
+ attr_accessor :variable_hash
25
+
26
+ define_method_handler(:process, :priority => 1) {|tree|
27
+ tree
28
+ }.condition{|tree| (tree.node_type == :iter and tree[1][2] == :_static) or (tree.node_type == :call and tree[2] == :infer) }
29
+
30
+ define_method_handler(:process, :priority => -1) {|tree|
31
+ tree.map{|subtree| process subtree}
32
+ }.condition{|tree| tree.respond_to?(:node_type) }
33
+
34
+ define_method_handler(:process, :priority => 1) {|tree|
35
+ tree
36
+ }.condition{|tree| not tree.respond_to?(:node_type) }
37
+
38
+ define_method_handler(:process) {|tree|
39
+ fs(:call, tree, :infer, fs(:arglist, fs(:const, variable_hash[tree[1]].to_s.to_sym)))
40
+ }.condition{|tree| tree.node_type == :lvar and variable_hash[tree[1]] and variable_hash[tree[1]] != :dynamic}
41
+
42
+ end
43
+
44
+ def initialize(inferencer)
45
+ @inferencer = inferencer
46
+ end
47
+
48
+ def call(tree)
49
+ variable_hash = Hash.new
50
+
51
+ # search variable assignments
52
+ tree.walk_tree do |subtree|
53
+ if subtree.node_type == :lasgn and subtree[2]
54
+ types = @inferencer.infer(subtree[2])
55
+ lname = subtree[1]
56
+ if types.size == 1
57
+ if variable_hash[lname]
58
+ if variable_hash[lname] != types.first
59
+ variable_hash[lname] = :dynamic
60
+ end
61
+ else
62
+ variable_hash[lname] = types.first
63
+ end
64
+ elsif types.size == 0
65
+ variable_hash[lname] = :dynamic
66
+ end
67
+ end
68
+ end
69
+
70
+ add_infer = AddInfer.new
71
+ add_infer.variable_hash = variable_hash
72
+
73
+ add_infer.process(tree)
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,76 @@
1
+ =begin
2
+
3
+ This file is part of the fastruby project, http://github.com/tario/fastruby
4
+
5
+ Copyright (c) 2011 Roberto Dario Seminara <robertodarioseminara@gmail.com>
6
+
7
+ fastruby is free software: you can redistribute it and/or modify
8
+ it under the terms of the gnu general public license as published by
9
+ the free software foundation, either version 3 of the license, or
10
+ (at your option) any later version.
11
+
12
+ fastruby is distributed in the hope that it will be useful,
13
+ but without any warranty; without even the implied warranty of
14
+ merchantability or fitness for a particular purpose. see the
15
+ gnu general public license for more details.
16
+
17
+ you should have received a copy of the gnu general public license
18
+ along with fastruby. if not, see <http://www.gnu.org/licenses/>.
19
+
20
+ =end
21
+ module FastRuby
22
+ class InferenceUpdater
23
+ class AddInfer
24
+ attr_accessor :variable_hash
25
+
26
+ define_method_handler(:process, :priority => 1) {|tree|
27
+ tree
28
+ }.condition{|tree| (tree.node_type == :iter and tree[1][2] == :_static) or (tree.node_type == :call and tree[2] == :infer) }
29
+
30
+ define_method_handler(:process, :priority => -1) {|tree|
31
+ tree.map &method(:process)
32
+ }.condition{|tree| tree.respond_to?(:node_type) }
33
+
34
+ define_method_handler(:process, :priority => 1) {|tree|
35
+ tree
36
+ }.condition{|tree| not tree.respond_to?(:node_type) }
37
+
38
+ define_method_handler(:process) {|tree|
39
+ fs(:call, tree, :infer, fs(:arglist, fs(:const, variable_hash[tree[1]].to_s.to_sym)))
40
+ }.condition{|tree| tree.node_type == :lvar and variable_hash[tree[1]] and variable_hash[tree[1]] != :dynamic}
41
+
42
+ end
43
+
44
+ def initialize(inferencer)
45
+ @inferencer = inferencer
46
+ end
47
+
48
+ def call(tree)
49
+ variable_hash = Hash.new
50
+
51
+ # search variable assignments
52
+ tree.walk_tree do |subtree|
53
+ if subtree.node_type == :lasgn and subtree[2]
54
+ types = @inferencer.infer(subtree[2])
55
+ lname = subtree[1]
56
+ if types.size == 1
57
+ if variable_hash[lname]
58
+ if variable_hash[lname] != types.first
59
+ variable_hash[lname] = :dynamic
60
+ end
61
+ else
62
+ variable_hash[lname] = types.first
63
+ end
64
+ elsif types.size == 0
65
+ variable_hash[lname] = :dynamic
66
+ end
67
+ end
68
+ end
69
+
70
+ add_infer = AddInfer.new
71
+ add_infer.variable_hash = variable_hash
72
+
73
+ add_infer.process(tree)
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,38 @@
1
+ =begin
2
+
3
+ This file is part of the fastruby project, http://github.com/tario/fastruby
4
+
5
+ Copyright (c) 2011 Roberto Dario Seminara <robertodarioseminara@gmail.com>
6
+
7
+ fastruby is free software: you can redistribute it and/or modify
8
+ it under the terms of the gnu general public license as published by
9
+ the free software foundation, either version 3 of the license, or
10
+ (at your option) any later version.
11
+
12
+ fastruby is distributed in the hope that it will be useful,
13
+ but without any warranty; without even the implied warranty of
14
+ merchantability or fitness for a particular purpose. see the
15
+ gnu general public license for more details.
16
+
17
+ you should have received a copy of the gnu general public license
18
+ along with fastruby. if not, see <http://www.gnu.org/licenses/>.
19
+
20
+ =end
21
+ require "set"
22
+ require "sexp"
23
+ require "define_method_handler"
24
+ require "fastruby/modules"
25
+
26
+ module FastRuby
27
+ class Inferencer
28
+ define_method_handler(:infer, :priority => -1000) do |tree|
29
+ []
30
+ end
31
+
32
+ def self.define_infer_for(node_type, &blk)
33
+ define_method_handler(:infer, &blk).condition{|tree| tree.node_type == node_type}
34
+ end
35
+
36
+ FastRuby::Modules.load_all("inferencer")
37
+ end
38
+ end
@@ -18,51 +18,40 @@ you should have received a copy of the gnu general public license
18
18
  along with fastruby. if not, see <http://www.gnu.org/licenses/>.
19
19
 
20
20
  =end
21
- require "set"
22
- require "sexp"
23
21
  require "define_method_handler"
22
+ require "fastruby/modules"
24
23
 
25
24
  module FastRuby
26
25
  class Inliner
27
- attr_accessor :infer_lvar_map
28
- attr_accessor :infer_self
29
- attr_reader :extra_locals
30
- attr_reader :extra_inferences
31
26
  attr_reader :inlined_methods
32
-
33
- def initialize
34
- @extra_locals = Set.new
35
- @extra_inferences = Hash.new
27
+
28
+ def initialize(inferencer)
36
29
  @inlined_methods = Array.new
30
+ @inferencer = inferencer
37
31
  end
38
32
 
39
33
  define_method_handler(:inline, :priority => -1000) do |tree|
40
34
  FastRubySexp.from_sexp(tree)
41
35
  end
42
36
 
43
- Dir.glob(FastRuby.fastruby_load_path + "/fastruby/inliner/modules/**/*.rb").each do |path|
44
- require path
45
- end
46
-
47
- def add_local(local)
48
- @extra_locals << local
37
+ def call(*args)
38
+ if args.first.find_tree{|tree| tree.node_type == :call && tree[2] != :infer && infer_type(tree[1] || fs(:self))}
39
+ inline *args
40
+ else
41
+ args.first
42
+ end
49
43
  end
50
44
 
45
+ FastRuby::Modules.load_all("inliner")
46
+
51
47
  def infer_type(recv)
52
- if recv[0] == :call
53
- if recv[2] == :infer
54
- eval(recv[3].last.last.to_s)
55
- end
56
- elsif recv[0] == :lvar
57
- @infer_lvar_map[recv[1]]
58
- elsif recv[0] == :self
59
- @infer_self
60
- elsif recv[0] == :str or recv[0] == :lit
61
- recv[1].class
48
+ array = @inferencer.infer(recv).to_a
49
+
50
+ if array.size == 1
51
+ array[0]
62
52
  else
63
53
  nil
64
54
  end
65
55
  end
66
-
67
56
  end
68
57
  end
@@ -0,0 +1,60 @@
1
+ =begin
2
+
3
+ This file is part of the fastruby project, http://github.com/tario/fastruby
4
+
5
+ Copyright (c) 2011 Roberto Dario Seminara <robertodarioseminara@gmail.com>
6
+
7
+ fastruby is free software: you can redistribute it and/or modify
8
+ it under the terms of the gnu general public license as published by
9
+ the free software foundation, either version 3 of the license, or
10
+ (at your option) any later version.
11
+
12
+ fastruby is distributed in the hope that it will be useful,
13
+ but without any warranty; without even the implied warranty of
14
+ merchantability or fitness for a particular purpose. see the
15
+ gnu general public license for more details.
16
+
17
+ you should have received a copy of the gnu general public license
18
+ along with fastruby. if not, see <http://www.gnu.org/licenses/>.
19
+
20
+ =end
21
+ require "define_method_handler"
22
+ require "fastruby/modules"
23
+
24
+ module FastRuby
25
+ class Inliner
26
+ attr_reader :inlined_methods
27
+
28
+ def initialize(inferencer)
29
+ @inlined_methods = Array.new
30
+ @inferencer = inferencer
31
+ end
32
+
33
+ define_method_handler(:inline, :priority => -1000) do |tree|
34
+ FastRubySexp.from_sexp(tree)
35
+ end
36
+
37
+ def call(*args)
38
+
39
+ return args.first
40
+
41
+ if args.first.find_tree{|tree| tree.node_type == :call && tree[2] != :infer && infer_type(tree[1] || fs(:self))}
42
+ inline *args
43
+ else
44
+ args.first
45
+ end
46
+ end
47
+
48
+ FastRuby::Modules.load_all("inliner")
49
+
50
+ def infer_type(recv)
51
+ array = @inferencer.infer(recv).to_a
52
+
53
+ if array.size == 1
54
+ array[0]
55
+ else
56
+ nil
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,53 @@
1
+ =begin
2
+
3
+ This file is part of the fastruby project, http://github.com/tario/fastruby
4
+
5
+ Copyright (c) 2011 Roberto Dario Seminara <robertodarioseminara@gmail.com>
6
+
7
+ fastruby is free software: you can redistribute it and/or modify
8
+ it under the terms of the gnu general public license as published by
9
+ the free software foundation, either version 3 of the license, or
10
+ (at your option) any later version.
11
+
12
+ fastruby is distributed in the hope that it will be useful,
13
+ but without any warranty; without even the implied warranty of
14
+ merchantability or fitness for a particular purpose. see the
15
+ gnu general public license for more details.
16
+
17
+ you should have received a copy of the gnu general public license
18
+ along with fastruby. if not, see <http://www.gnu.org/licenses/>.
19
+
20
+ =end
21
+ module FastRuby
22
+ class LocalsInference
23
+ attr_accessor :infer_self
24
+ attr_accessor :infer_lvar_map
25
+
26
+ def call(arg)
27
+ if arg.find_tree{|subtree| subtree.node_type == :lvar && @infer_lvar_map[subtree[1]]}
28
+ transform_block = proc do |tree|
29
+ if tree.node_type == :lvar
30
+ next tree unless @infer_lvar_map
31
+ lvar_type = @infer_lvar_map[tree[1]]
32
+ next tree unless lvar_type
33
+ fs(:call, tree, :infer, fs(:arglist, fs(:const, lvar_type.to_s.to_sym)))
34
+ elsif tree.node_type == :iter
35
+ if tree[1][2] == :_static
36
+ tree
37
+ end
38
+ elsif tree.node_type == :call
39
+ if tree[2] == :infer
40
+ tree[1]
41
+ end
42
+ else
43
+ nil
44
+ end
45
+ end
46
+
47
+ arg.transform &transform_block
48
+ else
49
+ arg
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,43 @@
1
+ =begin
2
+
3
+ This file is part of the fastruby project, http://github.com/tario/fastruby
4
+
5
+ Copyright (c) 2011 Roberto Dario Seminara <robertodarioseminara@gmail.com>
6
+
7
+ fastruby is free software: you can redistribute it and/or modify
8
+ it under the terms of the gnu general public license as published by
9
+ the free software foundation, either version 3 of the license, or
10
+ (at your option) any later version.
11
+
12
+ fastruby is distributed in the hope that it will be useful,
13
+ but without any warranty; without even the implied warranty of
14
+ merchantability or fitness for a particular purpose. see the
15
+ gnu general public license for more details.
16
+
17
+ you should have received a copy of the gnu general public license
18
+ along with fastruby. if not, see <http://www.gnu.org/licenses/>.
19
+
20
+ =end
21
+ require "set"
22
+ require "sexp"
23
+ require "define_method_handler"
24
+ require "fastruby/modules"
25
+
26
+ module FastRuby
27
+ class LvarType
28
+ def initialize(inferencer)
29
+ @infer_lvar_map = Hash.new
30
+ @inferencer = inferencer
31
+ end
32
+
33
+ def call(*args)
34
+ if args.first.find_tree{|tree| tree.node_type == :call && tree[2] == :lvar_type}
35
+ process *args
36
+ else
37
+ args.first
38
+ end
39
+ end
40
+
41
+ FastRuby::Modules.load_all("lvar_type")
42
+ end
43
+ end
@@ -0,0 +1,44 @@
1
+ =begin
2
+
3
+ This file is part of the fastruby project, http://github.com/tario/fastruby
4
+
5
+ Copyright (c) 2011 Roberto Dario Seminara <robertodarioseminara@gmail.com>
6
+
7
+ fastruby is free software: you can redistribute it and/or modify
8
+ it under the terms of the gnu general public license as published by
9
+ the free software foundation, either version 3 of the license, or
10
+ (at your option) any later version.
11
+
12
+ fastruby is distributed in the hope that it will be useful,
13
+ but without any warranty; without even the implied warranty of
14
+ merchantability or fitness for a particular purpose. see the
15
+ gnu general public license for more details.
16
+
17
+ you should have received a copy of the gnu general public license
18
+ along with fastruby. if not, see <http://www.gnu.org/licenses/>.
19
+
20
+ =end
21
+ require "set"
22
+ require "sexp"
23
+ require "define_method_handler"
24
+ require "fastruby/modules"
25
+
26
+ module FastRuby
27
+ class LvarType
28
+ def initialize(inferencer)
29
+ @infer_lvar_map = Hash.new
30
+ @inferencer = inferencer
31
+ end
32
+
33
+ def call(*args)
34
+ p args.first
35
+ # if args.first.find_tree{|tree| tree.node_type == :call && tree[2] == :lvar_type}
36
+ # process *args
37
+ # else
38
+ args.first
39
+ # end
40
+ end
41
+
42
+ FastRuby::Modules.load_all("lvar_type")
43
+ end
44
+ end
@@ -0,0 +1,43 @@
1
+ =begin
2
+
3
+ This file is part of the fastruby project, http://github.com/tario/fastruby
4
+
5
+ Copyright (c) 2011 Roberto Dario Seminara <robertodarioseminara@gmail.com>
6
+
7
+ fastruby is free software: you can redistribute it and/or modify
8
+ it under the terms of the gnu general public license as published by
9
+ the free software foundation, either version 3 of the license, or
10
+ (at your option) any later version.
11
+
12
+ fastruby is distributed in the hope that it will be useful,
13
+ but without any warranty; without even the implied warranty of
14
+ merchantability or fitness for a particular purpose. see the
15
+ gnu general public license for more details.
16
+
17
+ you should have received a copy of the gnu general public license
18
+ along with fastruby. if not, see <http://www.gnu.org/licenses/>.
19
+
20
+ =end
21
+ require "define_method_handler"
22
+
23
+ module FastRuby
24
+ class Pipeline
25
+ def initialize
26
+ @array = Array.new
27
+ end
28
+
29
+ def << (object)
30
+ @array << object
31
+ end
32
+
33
+ def call(arg)
34
+
35
+ last_result = arg
36
+ @array.each do |processor|
37
+ last_result = processor.call(last_result)
38
+ end
39
+
40
+ last_result
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ =begin
2
+
3
+ This file is part of the fastruby project, http://github.com/tario/fastruby
4
+
5
+ Copyright (c) 2011 Roberto Dario Seminara <robertodarioseminara@gmail.com>
6
+
7
+ fastruby is free software: you can redistribute it and/or modify
8
+ it under the terms of the gnu general public license as published by
9
+ the free software foundation, either version 3 of the license, or
10
+ (at your option) any later version.
11
+
12
+ fastruby is distributed in the hope that it will be useful,
13
+ but without any warranty; without even the implied warranty of
14
+ merchantability or fitness for a particular purpose. see the
15
+ gnu general public license for more details.
16
+
17
+ you should have received a copy of the gnu general public license
18
+ along with fastruby. if not, see <http://www.gnu.org/licenses/>.
19
+
20
+ =end
21
+ require "define_method_handler"
22
+
23
+ module FastRuby
24
+ class Pipeline
25
+ def initialize
26
+ @array = Array.new
27
+ end
28
+
29
+ def << (object)
30
+ @array << object
31
+ end
32
+
33
+ def call(arg)
34
+
35
+ last_result = arg
36
+ @array.each do |processor|
37
+ last_result = processor.call(last_result)
38
+ end
39
+
40
+ last_result
41
+ end
42
+ end
43
+ end