rltk 3.0.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +21 -22
  3. data/lib/rltk/ast.rb +185 -118
  4. data/lib/rltk/cfg.rb +157 -103
  5. data/lib/rltk/cg/basic_block.rb +19 -19
  6. data/lib/rltk/cg/bindings.rb +16 -16
  7. data/lib/rltk/cg/builder.rb +129 -129
  8. data/lib/rltk/cg/context.rb +7 -7
  9. data/lib/rltk/cg/contractor.rb +7 -7
  10. data/lib/rltk/cg/execution_engine.rb +30 -30
  11. data/lib/rltk/cg/function.rb +37 -37
  12. data/lib/rltk/cg/generated_bindings.rb +3932 -3932
  13. data/lib/rltk/cg/generic_value.rb +17 -17
  14. data/lib/rltk/cg/instruction.rb +116 -116
  15. data/lib/rltk/cg/llvm.rb +22 -22
  16. data/lib/rltk/cg/memory_buffer.rb +7 -7
  17. data/lib/rltk/cg/module.rb +73 -73
  18. data/lib/rltk/cg/pass_manager.rb +35 -35
  19. data/lib/rltk/cg/target.rb +41 -41
  20. data/lib/rltk/cg/triple.rb +7 -7
  21. data/lib/rltk/cg/type.rb +75 -75
  22. data/lib/rltk/cg/value.rb +161 -161
  23. data/lib/rltk/lexer.rb +57 -57
  24. data/lib/rltk/lexers/calculator.rb +7 -7
  25. data/lib/rltk/lexers/ebnf.rb +5 -5
  26. data/lib/rltk/parser.rb +338 -295
  27. data/lib/rltk/parsers/infix_calc.rb +7 -7
  28. data/lib/rltk/parsers/postfix_calc.rb +3 -3
  29. data/lib/rltk/parsers/prefix_calc.rb +3 -3
  30. data/lib/rltk/token.rb +13 -13
  31. data/lib/rltk/version.rb +6 -6
  32. data/test/cg/tc_basic_block.rb +17 -17
  33. data/test/cg/tc_control_flow.rb +41 -41
  34. data/test/cg/tc_function.rb +4 -4
  35. data/test/cg/tc_generic_value.rb +3 -3
  36. data/test/cg/tc_instruction.rb +53 -53
  37. data/test/cg/tc_math.rb +12 -12
  38. data/test/cg/tc_module.rb +14 -14
  39. data/test/cg/tc_transforms.rb +11 -11
  40. data/test/cg/tc_type.rb +12 -12
  41. data/test/cg/tc_value.rb +35 -35
  42. data/test/cg/ts_cg.rb +5 -5
  43. data/test/tc_ast.rb +137 -60
  44. data/test/tc_cfg.rb +34 -34
  45. data/test/tc_lexer.rb +42 -42
  46. data/test/tc_parser.rb +250 -173
  47. data/test/tc_token.rb +2 -2
  48. data/test/ts_rltk.rb +8 -8
  49. metadata +84 -85
  50. data/lib/rltk/cg/old_generated_bindings.rb +0 -6152
@@ -15,28 +15,28 @@ require 'rltk/parser'
15
15
  #######################
16
16
 
17
17
  module RLTK
18
-
18
+
19
19
  # The RLTK::Parsers module contains the parsers that are included as part
20
20
  # of the RLKT project.
21
21
  module Parsers
22
-
22
+
23
23
  # A parser for a simple infix calculator.
24
24
  class InfixCalc < Parser
25
-
25
+
26
26
  left :PLS, :SUB
27
27
  right :MUL, :DIV
28
-
28
+
29
29
  production(:e) do
30
30
  clause('NUM') { |n| n }
31
-
31
+
32
32
  clause('LPAREN e RPAREN') { |_, e, _| e }
33
-
33
+
34
34
  clause('e PLS e') { |e0, _, e1| e0 + e1 }
35
35
  clause('e SUB e') { |e0, _, e1| e0 - e1 }
36
36
  clause('e MUL e') { |e0, _, e1| e0 * e1 }
37
37
  clause('e DIV e') { |e0, _, e1| e0 / e1 }
38
38
  end
39
-
39
+
40
40
  finalize
41
41
  end
42
42
  end
@@ -16,18 +16,18 @@ require 'rltk/parser'
16
16
 
17
17
  module RLTK
18
18
  module Parsers
19
-
19
+
20
20
  # A parser for a simple post-fix calculator.
21
21
  class PostfixCalc < Parser
22
22
  production(:e) do
23
23
  clause('NUM') { |n| n }
24
-
24
+
25
25
  clause('e e PLS') { |e0, e1, _| e0 + e1 }
26
26
  clause('e e SUB') { |e0, e1, _| e0 - e1 }
27
27
  clause('e e MUL') { |e0, e1, _| e0 * e1 }
28
28
  clause('e e DIV') { |e0, e1, _| e0 / e1 }
29
29
  end
30
-
30
+
31
31
  finalize
32
32
  end
33
33
  end
@@ -16,18 +16,18 @@ require 'rltk/parser'
16
16
 
17
17
  module RLTK
18
18
  module Parsers
19
-
19
+
20
20
  # A parser for a simple prefix calculator.
21
21
  class PrefixCalc < Parser
22
22
  production(:e) do
23
23
  clause('NUM') { |n| n }
24
-
24
+
25
25
  clause('PLS e e') { |_, e0, e1| e0 + e1 }
26
26
  clause('SUB e e') { |_, e0, e1| e0 - e1 }
27
27
  clause('MUL e e') { |_, e0, e1| e0 * e1 }
28
28
  clause('DIV e e') { |_, e0, e1| e0 / e1 }
29
29
  end
30
-
30
+
31
31
  finalize
32
32
  end
33
33
  end
@@ -13,21 +13,21 @@ module RLTK
13
13
  class StreamPosition
14
14
  # @return [Integer]
15
15
  attr_accessor :stream_offset
16
-
16
+
17
17
  # @return [Integer]
18
18
  attr_accessor :line_number
19
-
19
+
20
20
  # @return [Integer]
21
21
  attr_accessor :line_offset
22
-
22
+
23
23
  # @return [Integer]
24
24
  attr_accessor :length
25
-
25
+
26
26
  # @return [String]
27
27
  attr_accessor :file_name
28
-
28
+
29
29
  alias :start :line_offset
30
-
30
+
31
31
  # Instantiates a new StreamPosition object with the values specified.
32
32
  #
33
33
  # @param [Integer] stream_offset The position from the beginning of the stream.
@@ -43,19 +43,19 @@ module RLTK
43
43
  @file_name = file_name
44
44
  end
45
45
  end
46
-
46
+
47
47
  # The Token class is used to represent the output of a RLTK::Lexer and the
48
48
  # input of a RLTK::Parser.
49
49
  class Token
50
50
  # @return [Symbol]
51
51
  attr_reader :type
52
-
52
+
53
53
  # @return [Symbol]
54
54
  attr_reader :value
55
-
55
+
56
56
  # @return [StreamPosition] StreamPosition object associated with this token.
57
57
  attr_reader :position
58
-
58
+
59
59
  # Instantiates a new Token object with the values specified.
60
60
  #
61
61
  # @param [Symbol] type A symbol representing the type of this Token.
@@ -64,10 +64,10 @@ module RLTK
64
64
  def initialize(type, value = nil, position = nil)
65
65
  @type = type
66
66
  @value = value
67
-
67
+
68
68
  @position = position
69
69
  end
70
-
70
+
71
71
  # Compares one token to another. This only tests the token's *type*
72
72
  # and *value* and not the location of the token in its source.
73
73
  #
@@ -77,7 +77,7 @@ module RLTK
77
77
  def ==(other)
78
78
  self.type == other.type and self.value == other.value
79
79
  end
80
-
80
+
81
81
  # @return [String] String representing the tokens *type* and *value*.
82
82
  def to_s
83
83
  if value
@@ -1,11 +1,11 @@
1
- # Author: Chris Wailes <chris.wailes@gmail.com>
2
- # Project: Ruby Language Toolkit
3
- # Date: 2012/03/08
4
- # Description: This file specifies the version number of RLTK.
1
+ # Author: Chris Wailes <chris.wailes@gmail.com>
2
+ # Project: Ruby Language Toolkit
3
+ # Date: 2012/03/08
4
+ # Description: This file specifies the version number of RLTK.
5
5
 
6
6
  module RLTK
7
7
  # The version number of the RLTK library.
8
- VERSION = '3.0.0'
8
+ VERSION = '3.0.1'
9
9
  # The version of LLVM targeted by RLTK.
10
- LLVM_TARGET_VERSION = '3.4'
10
+ LLVM_TARGET_VERSION = '3.4'
11
11
  end
@@ -18,64 +18,64 @@ require 'rltk/cg/module'
18
18
  class BasicBlockTester < Minitest::Test
19
19
  def setup
20
20
  RLTK::CG::LLVM.init(:X86)
21
-
21
+
22
22
  @mod = RLTK::CG::Module.new('Testing Module')
23
23
  end
24
-
24
+
25
25
  def test_basic_block
26
26
  fun = @mod.functions.add('basic_block_tester', RLTK::CG::VoidType, [])
27
27
  bb0 = fun.blocks.append
28
28
  bb1 = fun.blocks.append
29
-
29
+
30
30
  assert_equal(fun, bb0.parent)
31
31
  assert_equal(fun, bb1.parent)
32
-
32
+
33
33
  assert_equal(bb1, bb0.next)
34
34
  assert_equal(bb0, bb1.previous)
35
-
35
+
36
36
  bb0.build { br(bb1) }
37
37
  bb1.build { ret_void }
38
-
38
+
39
39
  assert_equal(bb0.instructions.first, bb0.instructions.last)
40
40
  assert_equal(bb1.instructions.first, bb1.instructions.last)
41
41
  end
42
-
42
+
43
43
  def test_basic_block_collection
44
44
  fun = @mod.functions.add('basic_block_collection_tester', RLTK::CG::VoidType, [])
45
45
  bb0 = fun.blocks.append
46
-
46
+
47
47
  assert_instance_of(RLTK::CG::BasicBlock, bb0)
48
-
48
+
49
49
  assert_equal(1, fun.blocks.size)
50
50
  assert_equal(fun.blocks.first, fun.blocks.last)
51
51
  assert_equal(fun.blocks.first, fun.blocks.entry)
52
-
52
+
53
53
  bb1 = fun.blocks.append
54
-
54
+
55
55
  assert_equal(2, fun.blocks.size)
56
56
  assert_equal(bb0, fun.blocks.first)
57
57
  assert_equal(bb1, fun.blocks.last)
58
-
58
+
59
59
  [fun.blocks.each.to_a, fun.blocks.to_a].each do |blocks|
60
60
  assert_equal(2, blocks.size)
61
61
  assert_equal(bb0, blocks[0])
62
62
  assert_equal(bb1, blocks[1])
63
63
  end
64
64
  end
65
-
65
+
66
66
  def test_basic_block_enumeration
67
67
  fun = @mod.functions.add('basic_block_enumeration_tester', RLTK::CG::DoubleType, [RLTK::CG::DoubleType])
68
68
  bb0 = fun.blocks.append
69
-
69
+
70
70
  [bb0.instructions.each.to_a, bb0.instructions.to_a].each do |insts|
71
71
  assert_equal(0, insts.size)
72
72
  end
73
-
73
+
74
74
  bb0.build { ret(fadd(fun.params[0], RLTK::CG::Double.new(1.0))) }
75
-
75
+
76
76
  [bb0.instructions.each.to_a, bb0.instructions.to_a].each do |insts|
77
77
  assert_equal(2, insts.size)
78
-
78
+
79
79
  assert_equal(bb0.instructions.first, insts[0])
80
80
  assert_equal(bb0.instructions.last, insts[1])
81
81
  end
@@ -20,171 +20,171 @@ require 'rltk/cg/type'
20
20
  class ControlFlowTester < Minitest::Test
21
21
  def setup
22
22
  RLTK::CG::LLVM.init(:X86)
23
-
23
+
24
24
  @mod = RLTK::CG::Module.new('Testing Module')
25
25
  @jit = RLTK::CG::JITCompiler.new(@mod)
26
26
  end
27
-
27
+
28
28
  ##############
29
29
  # Call Tests #
30
30
  ##############
31
-
31
+
32
32
  def test_external_call
33
33
  extern = @mod.functions.add('abs', RLTK::CG::NativeIntType, [RLTK::CG::NativeIntType])
34
-
34
+
35
35
  fun = @mod.functions.add('external_call_tester', RLTK::CG::NativeIntType, [RLTK::CG::NativeIntType]) do |fun|
36
36
  blocks.append { ret call(extern, fun.params[0]) }
37
37
  end
38
-
38
+
39
39
  assert_equal(10, @jit.run_function(fun, -10).to_i)
40
40
  end
41
-
41
+
42
42
  def test_external_string_call
43
43
  global = @mod.globals.add(RLTK::CG::ArrayType.new(RLTK::CG::Int8Type, 5), "path")
44
44
  global.linkage = :internal
45
45
  global.initializer = RLTK::CG::ConstantString.new('PATH')
46
-
46
+
47
47
  external = @mod.functions.add('getenv', RLTK::CG::PointerType.new(RLTK::CG::Int8Type), [RLTK::CG::PointerType.new(RLTK::CG::Int8Type)])
48
-
48
+
49
49
  fun = @mod.functions.add('external_string_call_tester', RLTK::CG::PointerType.new(RLTK::CG::Int8Type), []) do
50
50
  blocks.append do
51
51
  param = gep(global, [RLTK::CG::NativeInt.new(0), RLTK::CG::NativeInt.new(0)])
52
-
52
+
53
53
  ret call(external, param)
54
54
  end
55
55
  end
56
-
56
+
57
57
  assert_equal(ENV['PATH'], @jit.run_function(fun).ptr.read_pointer.read_string)
58
58
  end
59
-
59
+
60
60
  def test_nested_call
61
61
  fun0 = @mod.functions.add('simple_call_tester0', RLTK::CG::NativeIntType, []) do
62
62
  blocks.append { ret RLTK::CG::NativeInt.new(1) }
63
63
  end
64
-
64
+
65
65
  fun1 = @mod.functions.add('simple_call_tester1', RLTK::CG::NativeIntType, []) do
66
66
  blocks.append { ret call(fun0) }
67
67
  end
68
-
68
+
69
69
  assert_equal(1, @jit.run_function(fun1).to_i)
70
70
  end
71
-
71
+
72
72
  def test_recursive_call
73
73
  fun = @mod.functions.add('recursive_call_tester', RLTK::CG::NativeIntType, [RLTK::CG::NativeIntType]) do |fun|
74
74
  entry = blocks.append
75
75
  recurse = blocks.append
76
76
  exit = blocks.append
77
-
77
+
78
78
  entry.build do
79
79
  cond(icmp(:uge, fun.params[0], RLTK::CG::NativeInt.new(5)), exit, recurse)
80
80
  end
81
-
81
+
82
82
  result =
83
83
  recurse.build do
84
84
  call(fun, add(fun.params[0], RLTK::CG::NativeInt.new(1))).tap { br exit }
85
85
  end
86
-
86
+
87
87
  exit.build do
88
88
  ret(phi(RLTK::CG::NativeIntType, {entry => fun.params[0], recurse => result}))
89
89
  end
90
90
  end
91
-
91
+
92
92
  assert_equal(5, @jit.run_function(fun, 1).to_i)
93
93
  assert_equal(6, @jit.run_function(fun, 6).to_i)
94
94
  end
95
-
95
+
96
96
  ##############
97
97
  # Jump Tests #
98
98
  ##############
99
-
99
+
100
100
  def test_cond_jump
101
101
  fun = @mod.functions.add('direct_jump_tester', RLTK::CG::NativeIntType, []) do |fun|
102
102
  entry = blocks.append
103
-
103
+
104
104
  bb0 = blocks.append { ret RLTK::CG::NativeInt.new(1) }
105
105
  bb1 = blocks.append { ret RLTK::CG::NativeInt.new(0) }
106
-
106
+
107
107
  entry.build do
108
108
  cond(icmp(:eq, RLTK::CG::NativeInt.new(1), RLTK::CG::NativeInt.new(2)), bb0, bb1)
109
109
  end
110
110
  end
111
-
111
+
112
112
  assert_equal(0, @jit.run_function(fun).to_i)
113
113
  end
114
-
114
+
115
115
  def test_direct_jump
116
116
  fun = @mod.functions.add('direct_jump_tester', RLTK::CG::NativeIntType, []) do |fun|
117
117
  entry = blocks.append
118
-
118
+
119
119
  bb0 = blocks.append { ret(RLTK::CG::NativeInt.new(1)) }
120
120
  bb1 = blocks.append { ret(RLTK::CG::NativeInt.new(0)) }
121
-
121
+
122
122
  entry.build { br bb1 }
123
123
  end
124
-
124
+
125
125
  assert_equal(0, @jit.run_function(fun).to_i)
126
126
  end
127
-
127
+
128
128
  def test_switched_jump
129
129
  fun = @mod.functions.add('direct_jump_tester', RLTK::CG::NativeIntType, []) do |fun|
130
130
  entry = blocks.append
131
-
131
+
132
132
  bb0 = blocks.append { ret RLTK::CG::NativeInt.new(1) }
133
133
  bb1 = blocks.append { ret RLTK::CG::NativeInt.new(0) }
134
-
134
+
135
135
  entry.build do
136
136
  switch(RLTK::CG::NativeInt.new(1), bb0, {RLTK::CG::NativeInt.new(1) => bb1})
137
137
  end
138
138
  end
139
-
139
+
140
140
  assert_equal(0, @jit.run_function(fun).to_i)
141
141
  end
142
-
142
+
143
143
  ##############
144
144
  # Misc Tests #
145
145
  ##############
146
-
146
+
147
147
  def test_select
148
148
  fun = @mod.functions.add('select_tester', RLTK::CG::Int1Type, [RLTK::CG::NativeIntType]) do |fun|
149
149
  blocks.append do
150
150
  ret select(fun.params[0], RLTK::CG::Int1.new(0), RLTK::CG::Int1.new(1))
151
151
  end
152
152
  end
153
-
153
+
154
154
  assert_equal(0, @jit.run_function(fun, 1).to_i(false))
155
155
  assert_equal(1, @jit.run_function(fun, 0).to_i(false))
156
156
  end
157
-
157
+
158
158
  #############
159
159
  # Phi Tests #
160
160
  #############
161
-
161
+
162
162
  def test_phi
163
163
  fun = @mod.functions.add('phi_tester', RLTK::CG::NativeIntType, [RLTK::CG::NativeIntType]) do |fun|
164
164
  entry = blocks.append('entry')
165
165
  block0 = blocks.append('block0')
166
166
  block1 = blocks.append('block1')
167
167
  exit = blocks.append('exit')
168
-
168
+
169
169
  entry.build do
170
170
  cond(icmp(:eq, fun.params[0], RLTK::CG::NativeInt.new(0)), block0, block1)
171
171
  end
172
-
172
+
173
173
  result0 =
174
174
  block0.build do
175
175
  add(fun.params[0], RLTK::CG::NativeInt.new(1)).tap { br(exit) }
176
176
  end
177
-
177
+
178
178
  result1 =
179
179
  block1.build do
180
180
  sub(fun.params[0], RLTK::CG::NativeInt.new(1)).tap { br(exit) }
181
181
  end
182
-
182
+
183
183
  exit.build do
184
184
  ret(phi(RLTK::CG::NativeIntType, {block0 => result0, block1 => result1}))
185
185
  end
186
186
  end
187
-
187
+
188
188
  assert_equal(1, @jit.run_function(fun, 0).to_i)
189
189
  assert_equal(0, @jit.run_function(fun, 1).to_i)
190
190
  end