rltk 3.0.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -20,20 +20,20 @@ class FunctionTester < Minitest::Test
20
20
  def setup
21
21
  @mod = RLTK::CG::Module.new('Testing Module')
22
22
  @fun = @mod.functions.add('testing_function', RLTK::CG::NativeIntType, [RLTK::CG::NativeIntType, RLTK::CG::NativeIntType])
23
-
23
+
24
24
  @fun.params[0].name = 'foo'
25
25
  @fun.params[1].name = 'bar'
26
26
  end
27
-
27
+
28
28
  def test_equality
29
29
  fun0 = @mod.functions.add('fun0', RLTK::CG::NativeIntType, [])
30
30
  fun1 = @mod.functions.add('fun0', RLTK::CG::FloatType, [])
31
31
  fun2 = RLTK::CG::Function.new(fun0.ptr)
32
-
32
+
33
33
  assert_equal(fun0, fun2)
34
34
  refute_equal(fun0, fun1)
35
35
  end
36
-
36
+
37
37
  def test_positive_index_in_range
38
38
  assert_equal('foo', @fun.params[0].name)
39
39
  assert_equal('bar', @fun.params[1].name)
@@ -18,15 +18,15 @@ class GenericValueTester < Minitest::Test
18
18
  def setup
19
19
  RLTK::CG::LLVM.init(:X86)
20
20
  end
21
-
21
+
22
22
  def test_integer
23
23
  assert_equal(2, RLTK::CG::GenericValue.new(2).to_i)
24
24
  end
25
-
25
+
26
26
  def test_float
27
27
  assert_in_delta(3.1415926, RLTK::CG::GenericValue.new(3.1415926).to_f, 1e-6)
28
28
  end
29
-
29
+
30
30
  def test_double
31
31
  assert_in_delta(3.1415926, RLTK::CG::GenericValue.new(3.1415926, RLTK::CG::DoubleType).to_f(RLTK::CG::DoubleType), 1e-6)
32
32
  end
@@ -19,11 +19,11 @@ require 'rltk/cg/instruction'
19
19
  class InstructionTester < Minitest::Test
20
20
  def setup
21
21
  RLTK::CG::LLVM.init(:X86)
22
-
22
+
23
23
  @mod = RLTK::CG::Module.new('Testing Module')
24
24
  @jit = RLTK::CG::JITCompiler.new(@mod)
25
25
  end
26
-
26
+
27
27
  def test_float_comparison
28
28
  fcmp_assert(:oeq, 1.0, 1.0, true )
29
29
  fcmp_assert(:one, 1.0, 1.0, false)
@@ -40,29 +40,29 @@ class InstructionTester < Minitest::Test
40
40
  fcmp_assert(:ule, 1.0, 2.0, true )
41
41
  fcmp_assert(:uno, 1.0, 2.0, false)
42
42
  end
43
-
43
+
44
44
  def test_instruction
45
45
  fun = @mod.functions.add('instruction_tester', RLTK::CG::DoubleType, [RLTK::CG::DoubleType]) do |fun|
46
46
  blocks.append do
47
47
  ret(fadd(fun.params[0], RLTK::CG::Double.new(3.0)))
48
48
  end
49
49
  end
50
-
50
+
51
51
  entry = fun.blocks.entry
52
-
52
+
53
53
  inst0 = entry.instructions.first
54
54
  inst1 = entry.instructions.last
55
-
55
+
56
56
  assert_kind_of(RLTK::CG::Instruction, inst0)
57
57
  assert_kind_of(RLTK::CG::Instruction, inst1)
58
-
58
+
59
59
  assert_equal(inst1, inst0.next)
60
60
  assert_equal(inst0, inst1.previous)
61
-
61
+
62
62
  assert_equal(entry, inst0.parent)
63
63
  assert_equal(entry, inst1.parent)
64
64
  end
65
-
65
+
66
66
  def test_integer_comparison
67
67
  icmp_assert(:eq, 1, 1, true, true )
68
68
  icmp_assert(:ne, 1, 1, true, false)
@@ -75,182 +75,182 @@ class InstructionTester < Minitest::Test
75
75
  icmp_assert(:slt, -1, 2, true, true )
76
76
  icmp_assert(:sle, -1, 2, true, true )
77
77
  end
78
-
78
+
79
79
  def test_array_memory_access
80
80
  fun = @mod.functions.add('array_memory_access_tester', RLTK::CG::NativeIntType,
81
81
  [RLTK::CG::NativeIntType, RLTK::CG::NativeIntType]) do |fun|
82
-
82
+
83
83
  blocks.append do
84
84
  ptr = array_alloca(RLTK::CG::NativeIntType, RLTK::CG::NativeInt.new(2))
85
-
85
+
86
86
  store(fun.params[0], gep(ptr, [RLTK::CG::NativeInt.new(0)]))
87
87
  store(fun.params[1], gep(ptr, [RLTK::CG::NativeInt.new(1)]))
88
-
88
+
89
89
  ret(add(load(gep(ptr, [RLTK::CG::NativeInt.new(0)])), load(gep(ptr, [RLTK::CG::NativeInt.new(1)]))))
90
90
  end
91
91
  end
92
-
92
+
93
93
  assert_equal(3, @jit.run_function(fun, 1, 2).to_i)
94
94
  end
95
-
95
+
96
96
  def test_simple_memory_access
97
97
  fun = @mod.functions.add('simple_memory_access_tester', RLTK::CG::NativeIntType,
98
98
  [RLTK::CG::NativeIntType, RLTK::CG::NativeIntType]) do |fun|
99
-
99
+
100
100
  blocks.append do
101
101
  p0 = alloca(RLTK::CG::NativeIntType)
102
102
  p1 = alloca(RLTK::CG::NativeIntType)
103
-
103
+
104
104
  store(fun.params[0], p0)
105
105
  store(fun.params[1], p1)
106
-
106
+
107
107
  ret(add(load(p0), load(p1)))
108
108
  end
109
109
  end
110
-
110
+
111
111
  assert_equal(3, @jit.run_function(fun, 1, 2).to_i)
112
112
  end
113
-
113
+
114
114
  def test_struct_access
115
115
  fun = @mod.functions.add('struct_access_tester', RLTK::CG::FloatType, [RLTK::CG::NativeIntType, RLTK::CG::FloatType]) do |fun|
116
116
  blocks.append do
117
117
  st0 = RLTK::CG::StructType.new([RLTK::CG::NativeIntType, RLTK::CG::FloatType])
118
118
  st1 = RLTK::CG::StructType.new([RLTK::CG::FloatType, st0, RLTK::CG::NativeIntType])
119
-
119
+
120
120
  ptr = alloca(st1)
121
-
121
+
122
122
  store(fun.params[0], gep(ptr, [RLTK::CG::NativeInt.new(0), RLTK::CG::NativeInt.new(1), RLTK::CG::NativeInt.new(0)]))
123
123
  store(fun.params[1], gep(ptr, [RLTK::CG::NativeInt.new(0), RLTK::CG::NativeInt.new(1), RLTK::CG::NativeInt.new(1)]))
124
-
124
+
125
125
  addr0 = gep(ptr, [RLTK::CG::NativeInt.new(0), RLTK::CG::NativeInt.new(1), RLTK::CG::NativeInt.new(0)])
126
126
  addr1 = gep(ptr, [RLTK::CG::NativeInt.new(0), RLTK::CG::NativeInt.new(1), RLTK::CG::NativeInt.new(1)])
127
-
127
+
128
128
  ret(fadd(ui2fp(load(addr0), RLTK::CG::FloatType), load(addr1)))
129
129
  end
130
130
  end
131
-
131
+
132
132
  assert_in_delta(5.3, @jit.run_function(fun, 2, 3.3).to_f, 0.001)
133
133
  end
134
-
134
+
135
135
  def test_struct_values
136
136
  fun = @mod.functions.add('struct_values_tester', RLTK::CG::NativeIntType, [RLTK::CG::NativeIntType, RLTK::CG::NativeIntType]) do |fun|
137
137
  blocks.append do
138
138
  ptr = alloca(RLTK::CG::StructType.new([RLTK::CG::NativeIntType, RLTK::CG::NativeIntType]))
139
-
139
+
140
140
  struct = load(ptr)
141
141
  struct = insert_value(struct, fun.params[0], 0)
142
142
  struct = insert_value(struct, fun.params[1], 1)
143
-
143
+
144
144
  ret(add(extract_value(struct, 0), extract_value(struct, 1)))
145
145
  end
146
146
  end
147
-
147
+
148
148
  assert_equal(5, @jit.run_function(fun, 2, 3).to_i)
149
149
  end
150
-
150
+
151
151
  ####################
152
152
  # Conversion Tests #
153
153
  ####################
154
-
154
+
155
155
  def test_bitcast
156
156
  difftype_assert(:bitcast, RLTK::CG::Int8.new(255), RLTK::CG::Int8Type, :integer, -1)
157
157
  end
158
-
158
+
159
159
  def test_fp2ui
160
160
  difftype_assert(:fp2ui, RLTK::CG::Double.new(123.3), RLTK::CG::Int32Type, :integer, 123)
161
161
  difftype_assert(:fp2ui, RLTK::CG::Double.new(0.7), RLTK::CG::Int32Type, :integer, 0)
162
162
  difftype_assert(:fp2ui, RLTK::CG::Double.new(1.7), RLTK::CG::Int32Type, :integer, 1)
163
163
  end
164
-
164
+
165
165
  def test_fp2si
166
166
  difftype_assert(:fp2si, RLTK::CG::Double.new(-123.3), RLTK::CG::Int32Type, :integer, -123)
167
167
  difftype_assert(:fp2si, RLTK::CG::Double.new(0.7), RLTK::CG::Int32Type, :integer, 0)
168
168
  difftype_assert(:fp2si, RLTK::CG::Double.new(1.7), RLTK::CG::Int32Type, :integer, 1)
169
169
  end
170
-
170
+
171
171
  def test_fpext
172
172
  fconv_assert(:fp_ext, RLTK::CG::Float.new(123.0), RLTK::CG::DoubleType, 123.0)
173
173
  fconv_assert(:fp_ext, RLTK::CG::Float.new(123.0), RLTK::CG::FloatType, 123.0)
174
174
  end
175
-
175
+
176
176
  def test_fptrunc
177
177
  fconv_assert(:fp_trunc, RLTK::CG::Double.new(123.0), RLTK::CG::FloatType, 123.0)
178
178
  end
179
-
179
+
180
180
  def test_int64
181
181
  iconv_assert(:zext, RLTK::CG::Int64.new( 2**62 + 123), RLTK::CG::Int64Type, true, 2**62 + 123)
182
182
  iconv_assert(:zext, RLTK::CG::Int64.new(-2**62 - 123), RLTK::CG::Int64Type, true, -2**62 - 123)
183
183
  iconv_assert(:zext, RLTK::CG::Int64.new( 2**63 + 123), RLTK::CG::Int64Type, false, 2**63 + 123)
184
184
  end
185
-
185
+
186
186
  def test_sext
187
187
  iconv_assert(:sext, RLTK::CG::Int1.new(1), RLTK::CG::Int32Type, true, -1)
188
188
  iconv_assert(:sext, RLTK::CG::Int8.new(-1), RLTK::CG::Int16Type, false, 65535)
189
189
  end
190
-
190
+
191
191
  def test_si2fp
192
192
  difftype_assert(:si2fp, RLTK::CG::Int32.new(257), RLTK::CG::FloatType, :float, 257.0)
193
193
  difftype_assert(:si2fp, RLTK::CG::Int8.new(-1), RLTK::CG::DoubleType, :float, -1.0)
194
194
  end
195
-
195
+
196
196
  def test_truncate
197
197
  iconv_assert(:trunc, RLTK::CG::Int32.new(257), RLTK::CG::Int8Type, false, 1)
198
198
  iconv_assert(:trunc, RLTK::CG::Int32.new(123), RLTK::CG::Int1Type, false, 1)
199
199
  iconv_assert(:trunc, RLTK::CG::Int32.new(122), RLTK::CG::Int1Type, false, 0)
200
200
  end
201
-
201
+
202
202
  def test_ui2fp
203
203
  difftype_assert(:ui2fp, RLTK::CG::Int32.new(257), RLTK::CG::FloatType, :float, 257.0)
204
204
  difftype_assert(:ui2fp, RLTK::CG::Int8.new(-1), RLTK::CG::DoubleType, :float, 255.0)
205
205
  end
206
-
206
+
207
207
  def test_zext
208
208
  iconv_assert(:zext, RLTK::CG::Int16.new(257), RLTK::CG::Int32Type, false, 257)
209
209
  end
210
-
210
+
211
211
  ##################
212
212
  # Helper Methods #
213
213
  ##################
214
-
214
+
215
215
  def difftype_assert(op, operand, ret_type, assert_type, expected)
216
216
  res = run_convert(op, operand, ret_type)
217
-
217
+
218
218
  if assert_type == :integer then assert_equal(expected, res.to_i) else assert_in_delta(expected, res.to_f(ret_type), 0.001) end
219
219
  end
220
-
220
+
221
221
  def fcmp_assert(mode, operand0, operand1, expected)
222
222
  res = run_cmp(:fcmp, mode, RLTK::CG::Float.new(operand0), RLTK::CG::Float.new(operand1), RLTK::CG::Int1Type).to_i(false)
223
223
  assert_equal(expected.to_i, res)
224
224
  end
225
-
225
+
226
226
  def icmp_assert(mode, operand0, operand1, signed, expected)
227
227
  res = run_cmp(:icmp, mode, RLTK::CG::NativeInt.new(operand0, signed),
228
228
  RLTK::CG::NativeInt.new(operand1, signed), RLTK::CG::Int1Type).to_i(false)
229
-
229
+
230
230
  assert_equal(expected.to_i, res)
231
231
  end
232
-
232
+
233
233
  def fconv_assert(op, operand, ret_type, expected)
234
234
  assert_in_delta(expected, run_convert(op, operand, ret_type).to_f(ret_type), 0.001)
235
235
  end
236
-
236
+
237
237
  def iconv_assert(op, operand, ret_type, signed, expected)
238
238
  assert_equal(expected, run_convert(op, operand, ret_type).to_i(signed))
239
239
  end
240
-
240
+
241
241
  def run_cmp(op, mode, operand0, operand1, ret_type)
242
242
  fun = @mod.functions.add("#{op}_#{mode}_tester", ret_type, []) do
243
243
  blocks.append { ret(self.send(op, mode, operand0, operand1)) }
244
244
  end
245
-
245
+
246
246
  @jit.run_function(fun)
247
247
  end
248
-
248
+
249
249
  def run_convert(op, operand, ret_type)
250
250
  fun = @mod.functions.add("#{op}_tester", ret_type, []) do
251
251
  blocks.append { ret(self.send(op, operand, ret_type)) }
252
252
  end
253
-
253
+
254
254
  @jit.run_function(fun)
255
255
  end
256
256
  end
@@ -21,14 +21,14 @@ require 'rltk/cg/value'
21
21
  # Classes and Modules #
22
22
  #######################
23
23
 
24
- class MathTester < Minitest::Test
24
+ class MathTester < Minitest::Test
25
25
  def setup
26
26
  RLTK::CG::LLVM.init(:X86)
27
-
27
+
28
28
  @mod = RLTK::CG::Module.new('Testing Module')
29
29
  @jit = RLTK::CG::JITCompiler.new(@mod)
30
30
  end
31
-
31
+
32
32
  def test_integer_binary_operations
33
33
  int_binop_assert(:add, 3, 2, 3 + 2)
34
34
  int_binop_assert(:sub, 3, 2, 3 - 2)
@@ -38,7 +38,7 @@ class MathTester < Minitest::Test
38
38
  int_binop_assert(:urem, 10, 3, 10 % 3)
39
39
  int_binop_assert(:srem, 10, 3, 10 % 3)
40
40
  end
41
-
41
+
42
42
  def test_integer_bitwise_binary_operations
43
43
  int_binop_assert(:shl, 2, 3, 2 << 3)
44
44
  int_binop_assert(:lshr, 16, 3, 16 >> 3)
@@ -47,7 +47,7 @@ class MathTester < Minitest::Test
47
47
  int_binop_assert(:or, 2, 1, 2 | 1)
48
48
  int_binop_assert(:xor, 3, 2, 3 ^ 2)
49
49
  end
50
-
50
+
51
51
  def test_float_binary_operations
52
52
  float_binop_assert(:fadd, 3.1, 2.2, 3.1 + 2.2)
53
53
  float_binop_assert(:fsub, 3.1, 2.2, 3.1 - 2.2)
@@ -55,34 +55,34 @@ class MathTester < Minitest::Test
55
55
  float_binop_assert(:fdiv, 3.1, 2.2, 3.1 / 2.2)
56
56
  float_binop_assert(:frem, 3.1, 2.2, 3.1 % 2.2)
57
57
  end
58
-
58
+
59
59
  def test_simple_math_fun
60
60
  fun = @mod.functions.add('simple_math_tester', RLTK::CG::FloatType, [RLTK::CG::FloatType]) do |fun|
61
61
  blocks.append do
62
62
  ret(fadd(fun.params[0], RLTK::CG::Float.new(1.0)))
63
63
  end
64
64
  end
65
-
65
+
66
66
  assert_equal(6.0, @jit.run_function(fun, 5.0).to_f)
67
67
  end
68
-
68
+
69
69
  ##################
70
70
  # Helper Methods #
71
71
  ##################
72
-
72
+
73
73
  def float_binop_assert(op, operand0, operand1, expected)
74
74
  assert_in_delta(expected, run_binop(op, RLTK::CG::Float.new(operand0), RLTK::CG::Float.new(operand1), RLTK::CG::FloatType).to_f, 0.001)
75
75
  end
76
-
76
+
77
77
  def int_binop_assert(op, operand0, operand1, expected)
78
78
  assert_equal(expected, run_binop(op, RLTK::CG::NativeInt.new(operand0), RLTK::CG::NativeInt.new(operand1), RLTK::CG::NativeIntType).to_i)
79
79
  end
80
-
80
+
81
81
  def run_binop(op, operand0, operand1, ret_type)
82
82
  fun = @mod.functions.add(op.to_s + '_tester', ret_type, []) do
83
83
  blocks.append { ret(self.send(op, operand0, operand1)) }
84
84
  end
85
-
85
+
86
86
  @jit.run_function(fun)
87
87
  end
88
88
  end
@@ -27,63 +27,63 @@ require 'rltk/cg/value'
27
27
  class ModuleTester < Minitest::Test
28
28
  def setup
29
29
  RLTK::CG::LLVM.init(:X86)
30
-
30
+
31
31
  @mod = RLTK::CG::Module.new('Testing Module')
32
32
  @jit = RLTK::CG::JITCompiler.new(@mod)
33
-
33
+
34
34
  @mod.functions.add('int_function_tester', RLTK::CG::NativeIntType, []) do
35
35
  blocks.append { ret RLTK::CG::NativeInt.new(1) }
36
36
  end
37
37
  end
38
-
38
+
39
39
  def test_bitcode
40
40
  Tempfile.open('bitcode') do |tmp|
41
41
  assert(@mod.write_bitcode(tmp))
42
-
42
+
43
43
  new_mod = RLTK::CG::Module.read_bitcode(tmp.path)
44
44
  new_jit = RLTK::CG::JITCompiler.new(new_mod)
45
-
45
+
46
46
  assert_equal(1, new_jit.run_function(new_mod.functions['int_function_tester']).to_i)
47
47
  end
48
48
  end
49
-
49
+
50
50
  def test_equality
51
51
  mod0 = RLTK::CG::Module.new('foo')
52
52
  mod1 = RLTK::CG::Module.new('bar')
53
53
  mod2 = RLTK::CG::Module.new(mod0.ptr)
54
-
54
+
55
55
  assert_equal(mod0, mod2)
56
56
  refute_equal(mod0, mod1)
57
57
  end
58
-
58
+
59
59
  def test_external_fun
60
60
  fun = @mod.functions.add(:sin, RLTK::CG::DoubleType, [RLTK::CG::DoubleType])
61
61
  res = @jit.run_function(fun, RLTK::CG::GenericValue.new(1.0, RLTK::CG::DoubleType)).to_f(RLTK::CG::DoubleType)
62
-
62
+
63
63
  assert_in_delta(Math.sin(1.0), res, 1e-10)
64
64
  end
65
-
65
+
66
66
  def test_simple_int_fun
67
67
  assert_equal(1, @jit.run_function(@mod.functions['int_function_tester']).to_i)
68
68
  end
69
-
69
+
70
70
  def test_simple_float_fun
71
71
  fun = @mod.functions.add('float_function_tester', RLTK::CG::FloatType, []) do
72
72
  blocks.append do
73
73
  ret RLTK::CG::Float.new(1.5)
74
74
  end
75
75
  end
76
-
76
+
77
77
  assert_equal(1.5, @jit.run_function(fun).to_f(RLTK::CG::FloatType))
78
78
  end
79
-
79
+
80
80
  def test_simple_double_fun
81
81
  fun = @mod.functions.add('double_function_tester', RLTK::CG::DoubleType, []) do
82
82
  blocks.append do
83
83
  ret RLTK::CG::Double.new(1.6)
84
84
  end
85
85
  end
86
-
86
+
87
87
  assert_equal(1.6, @jit.run_function(fun).to_f(RLTK::CG::DoubleType))
88
88
  end
89
89
  end