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
@@ -16,23 +16,23 @@ require 'rltk/cg/type'
16
16
  #######################
17
17
 
18
18
  module RLTK::CG
19
-
19
+
20
20
  # GenericValue objects are used to pass parameters into
21
21
  # {ExecutionEngine ExecutionEngines} as well as retreive an evaluated
22
22
  # function's result. They may contain values of several different types:
23
- #
23
+ #
24
24
  # * Integer
25
25
  # * Float
26
26
  # * Boolean
27
27
  class GenericValue
28
28
  include BindingClass
29
-
29
+
30
30
  # The Proc object called by the garbage collector to free resources used by LLVM.
31
31
  CLASS_FINALIZER = Proc.new { |id| Bindings.dispose_generic_value(ptr) if ptr = ObjectSpace._id2ref(id).ptr }
32
-
32
+
33
33
  # @return [Type] LLVM type of this GenericValue.
34
34
  attr_reader :type
35
-
35
+
36
36
  # Creates a new GenericValue from a given Ruby value.
37
37
  #
38
38
  # @param [FFI::Pointer, Integer, ::Float, Boolean] ruby_val
@@ -43,50 +43,50 @@ module RLTK::CG
43
43
  case ruby_val
44
44
  when FFI::Pointer
45
45
  [ruby_val, nil]
46
-
46
+
47
47
  when ::Integer
48
48
  type = if type then check_cg_type(type, IntType) else NativeIntType.instance end
49
-
49
+
50
50
  [Bindings.create_generic_value_of_int(type, ruby_val, signed.to_i), type]
51
-
51
+
52
52
  when ::Float
53
53
  type = if type then check_cg_type(type, RealType) else FloatType.instance end
54
-
54
+
55
55
  [Bindings.create_generic_value_of_float(type, ruby_val), type]
56
-
56
+
57
57
  when TrueClass
58
58
  [Bindings.create_generic_value_of_int(Int1Type, 1, 0), Int1Type]
59
-
59
+
60
60
  when FalseClass
61
61
  [Bindings.create_generic_value_of_int(Int1Type, 0, 0), Int1Type]
62
62
  end
63
-
63
+
64
64
  # Define a finalizer to free the memory used by LLVM for this
65
65
  # generic value.
66
66
  ObjectSpace.define_finalizer(self, CLASS_FINALIZER)
67
67
  end
68
-
68
+
69
69
  # @param [Boolean] signed Treat the GenericValue as a signed integer.
70
70
  #
71
71
  # @return [Integer]
72
72
  def to_i(signed = true)
73
73
  val = Bindings.generic_value_to_int(@ptr, signed.to_i)
74
-
74
+
75
75
  if signed and val >= 2**63 then val - 2**64 else val end
76
76
  end
77
-
77
+
78
78
  # @param [FloatType] type Type of the real value stored in this GenericValue.
79
79
  #
80
80
  # @return [Float]
81
81
  def to_f(type = RLTK::CG::FloatType)
82
82
  Bindings.generic_value_to_float(@type || check_cg_type(type, RLTK::CG::NumberType), @ptr)
83
83
  end
84
-
84
+
85
85
  # @return [Boolean]
86
86
  def to_bool
87
87
  self.to_i(false).to_bool
88
88
  end
89
-
89
+
90
90
  # @return [FFI::Pointer] GenericValue as a pointer.
91
91
  def to_ptr_value
92
92
  Bindings.generic_value_to_pointer(@ptr)
@@ -16,10 +16,10 @@ require 'rltk/cg/value'
16
16
  #######################
17
17
 
18
18
  module RLTK::CG
19
-
19
+
20
20
  # This class represents LLVM IR instructions.
21
21
  class Instruction < User
22
-
22
+
23
23
  # Many of the C functions for interacting with instructions treat
24
24
  # all instructions as Instruction objects. However, some Instruction
25
25
  # sub-types can be tested for. This is a list of those sub-types and
@@ -55,7 +55,7 @@ module RLTK::CG
55
55
  :UIToFP,
56
56
  :Unreachable
57
57
  ]
58
-
58
+
59
59
  # Instantiate an Instruction object from a given pointer. The type
60
60
  # of the instruction is tested and the appropriate sub-type is picked
61
61
  # if possible. If not, a generic Instruction object is returned.
@@ -65,22 +65,22 @@ module RLTK::CG
65
65
  # @return [Instruction] An Instruction or one of its sub-types.
66
66
  def self.from_ptr(ptr)
67
67
  match = nil
68
-
68
+
69
69
  TESTABLE.each do |el|
70
70
  klass, test =
71
71
  if el.is_a?(Symbol)
72
72
  [RLTK::CG.const_get("#{el}Inst".to_sym), Bindings.get_bname("IsA#{el}Inst")]
73
-
73
+
74
74
  else
75
75
  [RLTK::CG.const_get("#{el.first}Inst".to_sym), Bindings.get_bname("IsA#{el.last}Inst")]
76
76
  end
77
-
77
+
78
78
  match = klass if Bindings.send(test, ptr)
79
79
  end
80
-
80
+
81
81
  if match then match else Intruction end.new(ptr)
82
82
  end
83
-
83
+
84
84
  # You should never instantiate Instruction object directly. Use the
85
85
  # builder class to add new instructions.
86
86
  #
@@ -88,38 +88,38 @@ module RLTK::CG
88
88
  def initialize(ptr)
89
89
  @ptr = check_type(ptr, FFI::Pointer, 'ptr')
90
90
  end
91
-
91
+
92
92
  # @return [Instruction, nil] Instruction that follows the current one in a {BasicBlock}.
93
93
  def next
94
94
  if (ptr = Bindings.get_next_instruction(@ptr)).null? then nil else Instruction.from_ptr(ptr) end
95
95
  end
96
-
96
+
97
97
  # @return [BasicBlock] BasicBlock that contains this Instruction.
98
98
  def parent
99
99
  if (ptr = Bindings.get_instruction_parent(@ptr)).null? then nil else BasicBlock.new(ptr) end
100
100
  end
101
-
101
+
102
102
  # @return [Instruction, nil] Instruction that precedes the current on in a {BasicBlock}.
103
103
  def previous
104
104
  if (ptr = Bindings.get_previous_instruction(@ptr)).null? then nil else Instruction.from_ptr(ptr) end
105
105
  end
106
-
106
+
107
107
  ##########################################
108
108
  # Instruction Testing Method Definitions #
109
109
  ##########################################
110
-
110
+
111
111
  selector = Regexp.new(/LLVMIsA.*Inst/)
112
112
  syms = Symbol.all_symbols.select { |sym| selector.match(sym.to_s) }
113
-
113
+
114
114
  syms.each do |sym|
115
115
  sym = (Bindings.get_bname(sym).to_s + '?').to_sym
116
-
116
+
117
117
  define_method(sym) do
118
118
  Bindings.send(sym, @ptr)
119
119
  end
120
120
  end
121
121
  end
122
-
122
+
123
123
  # An Instruction representing a function call.
124
124
  #
125
125
  # @LLVMInst call
@@ -132,7 +132,7 @@ module RLTK::CG
132
132
  def calling_convention
133
133
  Bindings.enum_type(:call_conv)[Bindings.get_instruction_call_conv(@ptr)]
134
134
  end
135
-
135
+
136
136
  # Set the calling convention used for this call.
137
137
  #
138
138
  # @see Bindings._enum_call_conv_
@@ -140,15 +140,15 @@ module RLTK::CG
140
140
  # @param [Symbol] conv Calling convention to set.
141
141
  def calling_convention=(conv)
142
142
  Bindings.set_instruction_call_conv(@ptr, Bindings.enum_type(:call_conv)[conv])
143
-
143
+
144
144
  conv
145
145
  end
146
-
146
+
147
147
  # @return [Boolean]
148
148
  def tail_call?
149
149
  Bindings.is_tail_call(@ptr).to_bool
150
150
  end
151
-
151
+
152
152
  # Sets the *tail call* property for this call instruction.
153
153
  #
154
154
  # @param [Boolean] bool If this is a tail call or not
@@ -158,7 +158,7 @@ module RLTK::CG
158
158
  Bindings.set_tail_call(@ptr, bool.to_i)
159
159
  end
160
160
  end
161
-
161
+
162
162
  # An Instruction representing a Phi node.
163
163
  #
164
164
  # @see http://en.wikipedia.org/wiki/Static_single_assignment_form
@@ -168,16 +168,16 @@ module RLTK::CG
168
168
  def incoming
169
169
  @incoming_collection ||= IncomingCollection.new(self)
170
170
  end
171
-
171
+
172
172
  # This class is used to access a Phi node's incoming {BasicBlock}/{Value} pairs.
173
173
  class IncomingCollection
174
174
  include Enumerable
175
-
175
+
176
176
  # @param [PhiInst] phi Phi instruction for which this is a proxy.
177
177
  def initialize(phi)
178
178
  @phi = phi
179
179
  end
180
-
180
+
181
181
  # Access the {BasicBlock}/{Value} pair at the given index.
182
182
  #
183
183
  # @param [Integer] index Index of the desired pair. May be negative.
@@ -185,12 +185,12 @@ module RLTK::CG
185
185
  # @return [Array(BasicBlock, Value)]
186
186
  def [](index)
187
187
  index += self.size if index < 0
188
-
188
+
189
189
  if 0 <= index and index < self.size
190
190
  [self.block(index), self.value(index)]
191
191
  end
192
192
  end
193
-
193
+
194
194
  # Add incoming {BasicBlock}/{Value} pairs to a Phi node.
195
195
  #
196
196
  # @example Adding a single block/value pair:
@@ -214,28 +214,28 @@ module RLTK::CG
214
214
  [(keys = overloaded.keys), overloaded.values_at(*keys)]
215
215
  end
216
216
  end
217
-
217
+
218
218
  vals_ptr = FFI::MemoryPointer.new(:pointer, vals.size)
219
219
  vals_ptr.write_array_of_pointer(vals)
220
-
220
+
221
221
  blks_ptr = FFI::MemoryPointer.new(:pointer, blks.size)
222
222
  blks_ptr.write_array_of_pointer(blks)
223
-
223
+
224
224
  nil.tap { Bindings.add_incoming(@phi, vals_ptr, blks_ptr, vals.length) }
225
225
  end
226
226
  alias :<< :add
227
-
227
+
228
228
  # @param [Integer] index Index of desired incoming {BasicBlock}.
229
229
  #
230
230
  # @return [BasicBlock] Incoming {BasicBlock}.
231
231
  def block(index)
232
232
  index += self.size if index < 0
233
-
233
+
234
234
  if 0 <= index and index < self.size
235
235
  BasicBlock.new(Bindings.get_incoming_block(@phi, index))
236
236
  end
237
237
  end
238
-
238
+
239
239
  # An iterator for each incoming {BasicBlock}/{Value} pair.
240
240
  #
241
241
  # @yieldparam pair [Array(BasicBlock, Value)]
@@ -243,30 +243,30 @@ module RLTK::CG
243
243
  # @return [Enumerator] Returns an Enumerator if no block is given.
244
244
  def each
245
245
  return to_enum(:each) unless block_given?
246
-
246
+
247
247
  self.size.times { |index| yield self[index] }
248
-
248
+
249
249
  self
250
250
  end
251
-
251
+
252
252
  # @return [Integer] Number of incoming {BasicBlock}/{Value} pairs.
253
253
  def size
254
254
  Bindings.count_incoming(@phi)
255
255
  end
256
-
256
+
257
257
  # @param [Integer] index Index of desired incoming {Value}.
258
258
  #
259
259
  # @return [BasicBlock] Incoming {Value}.
260
260
  def value(index)
261
261
  index += self.size if index < 0
262
-
262
+
263
263
  if 0 <= index and index < self.size
264
264
  Value.new(Bindings.get_incoming_value(@phi, index))
265
265
  end
266
266
  end
267
267
  end
268
268
  end
269
-
269
+
270
270
  # An Instruction representing a conditional jump with multiple cases.
271
271
  #
272
272
  # @LLVMInst switch
@@ -281,238 +281,238 @@ module RLTK::CG
281
281
  Bindings.add_case(@ptr, val, block)
282
282
  end
283
283
  end
284
-
284
+
285
285
  #############################
286
286
  # Empty Instruction Classes #
287
287
  #############################
288
-
288
+
289
289
  # @LLVMInst add
290
290
  class AddInst < Instruction; end
291
-
291
+
292
292
  # @LLVMInst addr_space_cast
293
293
  class AddrSpaceCastInst < Instruction; end
294
-
294
+
295
295
  # @LLVMInst alloca
296
296
  class AllocaInst < Instruction; end
297
-
297
+
298
298
  # @LLVMInst and
299
299
  class AndInst < Instruction; end
300
-
300
+
301
301
  # @LLVMInst ashr
302
302
  class ARightShiftInst < Instruction; end
303
-
303
+
304
304
  # @LLVMInst alloca
305
305
  class ArrayAllocaInst < Instruction; end
306
-
306
+
307
307
  class ArrayMallocInst < Instruction; end
308
-
308
+
309
309
  # @LLVMInst atomicrmw
310
310
  class AtomicRMWInst < Instruction; end
311
-
311
+
312
312
  # @LLVMInst bitcast
313
313
  class BitCastInst < Instruction; end
314
-
314
+
315
315
  # @LLVMInst br
316
316
  class BranchInst < Instruction; end
317
-
317
+
318
318
  # @LLVMInst br
319
319
  class CondBranchInst < Instruction; end
320
-
320
+
321
321
  # @LLVMInst sdiv
322
322
  class ExactSDivInst < Instruction; end
323
-
323
+
324
324
  # @LLVMInst extractelement
325
325
  class ExtractElementInst < Instruction; end
326
-
326
+
327
327
  # @LLVMInst extractvalue
328
328
  class ExtractValueInst < Instruction; end
329
-
329
+
330
330
  # @LLVMInst fadd
331
331
  class FAddInst < Instruction; end
332
-
332
+
333
333
  # @LLVMInst fcmp
334
334
  class FCmpInst < Instruction; end
335
-
335
+
336
336
  # @LLVMInst fdiv
337
337
  class FDivInst < Instruction; end
338
-
338
+
339
339
  # @LLVMInst fmul
340
340
  class FMulInst < Instruction; end
341
-
341
+
342
342
  # @LLVMInst fsub
343
343
  class FNegInst < Instruction; end
344
-
344
+
345
345
  # @LLVMInst fptosi
346
346
  class FPToSIInst < Instruction; end
347
-
347
+
348
348
  # @LLVMInst fptoui
349
349
  class FPToUIInst < Instruction; end
350
-
350
+
351
351
  class FPCastInst < Instruction; end
352
-
352
+
353
353
  # @LLVMInst fpext
354
354
  class FPExtendInst < Instruction; end
355
-
355
+
356
356
  # @LLVMInst fptrunc
357
357
  class FPTruncInst < Instruction; end
358
-
358
+
359
359
  class FreeInst < Instruction; end
360
-
360
+
361
361
  # @LLVMInst frem
362
362
  class FRemInst < Instruction; end
363
-
363
+
364
364
  # @LLVMInst fsub
365
365
  class FSubInst < Instruction; end
366
-
366
+
367
367
  # @LLVMInst gep
368
368
  # @see http://llvm.org/docs/GetElementPtr.html
369
369
  class GetElementPtrInst < Instruction; end
370
-
370
+
371
371
  class GlobalStringInst < Instruction; end
372
372
  class GlobalStringPtrInst < Instruction; end
373
-
373
+
374
374
  # @LLVMInst gep
375
375
  # @see http://llvm.org/docs/GetElementPtr.html
376
376
  class InBoundsGEPInst < Instruction; end
377
-
377
+
378
378
  # @LLVMInst insertelement
379
379
  class InsertElementInst < Instruction; end
380
-
380
+
381
381
  # @LLVMInst insertvalue
382
382
  class InsertValueInst < Instruction; end
383
-
383
+
384
384
  # @LLVMInst inttoptr
385
385
  class IntToPtrInst < Instruction; end
386
-
386
+
387
387
  class IntCastInst < Instruction; end
388
-
388
+
389
389
  # @LLVMInst icmp
390
390
  class IntCmpInst < Instruction; end
391
-
391
+
392
392
  # @LLVMInst invoke
393
393
  class InvokeInst < Instruction; end
394
-
394
+
395
395
  class IsNotNullInst < Instruction; end
396
396
  class IsNullInstInst < Instruction; end
397
-
397
+
398
398
  # @LLVMInst shl
399
399
  class LeftShiftInst < Instruction; end
400
-
400
+
401
401
  # @LLVMInst load
402
402
  class LoadInst < Instruction; end
403
-
403
+
404
404
  # @LLVMInst lshr
405
405
  class LRightShiftInst < Instruction; end
406
-
406
+
407
407
  class MallocInst < Instruction; end
408
-
408
+
409
409
  # @LLVMInst mul
410
410
  class MulInst < Instruction; end
411
-
411
+
412
412
  # @LLVMInst sub
413
413
  class NegInst < Instruction; end
414
-
414
+
415
415
  class NotInst < Instruction; end
416
-
416
+
417
417
  # @LLVMInst add
418
418
  class NSWAddInst < Instruction; end
419
-
419
+
420
420
  # @LLVMInst mul
421
421
  class NSWMulInst < Instruction; end
422
-
422
+
423
423
  # @LLVMInst sub
424
424
  class NSWNegInst < Instruction; end
425
-
425
+
426
426
  # @LLVMInst sub
427
427
  class NSWSubInst < Instruction; end
428
-
428
+
429
429
  # @LLVMInst add
430
430
  class NUWAddInst < Instruction; end
431
-
431
+
432
432
  # @LLVMInst mul
433
433
  class NUWMulInst < Instruction; end
434
-
434
+
435
435
  # @LLVMInst sub
436
436
  class NUWNegInst < Instruction; end
437
-
437
+
438
438
  # @LLVMInst sub
439
439
  class NUWSubInst < Instruction; end
440
-
440
+
441
441
  # @LLVMInst or
442
442
  class OrInst < Instruction; end
443
-
443
+
444
444
  # @LLVMInst ptrtoint
445
445
  class PtrToIntInst < Instruction; end
446
-
446
+
447
447
  class PtrCastInst < Instruction; end
448
448
  class PtrDiffInst < Instruction; end
449
-
449
+
450
450
  # @LLVMInst ret
451
451
  class ReturnInst < Instruction; end
452
-
452
+
453
453
  # @LLVMInst ret
454
454
  class ReturnAggregateInst < Instruction; end
455
-
455
+
456
456
  # @LLVMInst ret
457
457
  class ReturnVoidInst < Instruction; end
458
-
458
+
459
459
  # @LLVMInst sdiv
460
460
  class SDivInst < Instruction; end
461
-
461
+
462
462
  # @LLVMInst select
463
463
  class SelectInst < Instruction; end
464
-
464
+
465
465
  # @LLVMInst shufflevector
466
466
  class ShuffleVectorInst < Instruction; end
467
-
467
+
468
468
  # @LLVMInst sext
469
469
  class SignExtendInst < Instruction; end
470
-
470
+
471
471
  # @LLVMInst sext
472
472
  # @LLVMInst bitcast
473
473
  class SignExtendOrBitCastInst < Instruction; end
474
-
474
+
475
475
  # @LLVMInst sitofp
476
476
  class SIToFPInst < Instruction; end
477
-
477
+
478
478
  # @LLVMInst srem
479
479
  class SRemInst < Instruction; end
480
-
480
+
481
481
  # @LLVMInst store
482
482
  class StoreInst < Instruction; end
483
-
483
+
484
484
  # @LLVMInst gep
485
485
  # @see http://llvm.org/docs/GetElementPtr.html
486
486
  class StructGEPInst < Instruction; end
487
-
487
+
488
488
  # @LLVMInst sub
489
489
  class SubInst < Instruction; end
490
-
490
+
491
491
  # @LLVMInst trunc
492
492
  class TruncateInst < Instruction; end
493
-
493
+
494
494
  # @LLVMInst trunc
495
495
  # @LLVMInst bitcast
496
496
  class TruncateOrBitCastInst < Instruction; end
497
-
497
+
498
498
  # @LLVMInst udiv
499
499
  class UDivInst < Instruction; end
500
-
500
+
501
501
  # @LLVMInst uitofp
502
502
  class UIToFPInst < Instruction; end
503
-
503
+
504
504
  # @LLVMInst unreachable
505
505
  class UnreachableInst < Instruction; end
506
-
506
+
507
507
  # @LLVMInst urem
508
508
  class URemInst < Instruction; end
509
-
509
+
510
510
  # @LLVMInst xor
511
511
  class XOrInst < Instruction; end
512
-
512
+
513
513
  # @LLVMInst zext
514
514
  class ZeroExtendInst < Instruction; end
515
-
515
+
516
516
  # @LLVMInst zext
517
517
  # @LLVMInst bitcast
518
518
  class ZeroExtendOrBitCastInst < Instruction; end