rltk 2.2.1 → 3.0.0
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.
- checksums.yaml +7 -0
- data/LICENSE +12 -12
- data/README.md +458 -285
- data/Rakefile +99 -92
- data/lib/rltk/ast.rb +221 -126
- data/lib/rltk/cfg.rb +218 -239
- data/lib/rltk/cg/basic_block.rb +1 -1
- data/lib/rltk/cg/bindings.rb +9 -26
- data/lib/rltk/cg/builder.rb +40 -8
- data/lib/rltk/cg/context.rb +1 -1
- data/lib/rltk/cg/contractor.rb +51 -0
- data/lib/rltk/cg/execution_engine.rb +45 -8
- data/lib/rltk/cg/function.rb +12 -2
- data/lib/rltk/cg/generated_bindings.rb +2541 -575
- data/lib/rltk/cg/generic_value.rb +2 -2
- data/lib/rltk/cg/instruction.rb +104 -83
- data/lib/rltk/cg/llvm.rb +44 -3
- data/lib/rltk/cg/memory_buffer.rb +22 -5
- data/lib/rltk/cg/module.rb +85 -36
- data/lib/rltk/cg/old_generated_bindings.rb +6152 -0
- data/lib/rltk/cg/pass_manager.rb +87 -43
- data/lib/rltk/cg/support.rb +2 -4
- data/lib/rltk/cg/target.rb +158 -28
- data/lib/rltk/cg/triple.rb +8 -8
- data/lib/rltk/cg/type.rb +69 -25
- data/lib/rltk/cg/value.rb +107 -66
- data/lib/rltk/cg.rb +16 -17
- data/lib/rltk/lexer.rb +21 -11
- data/lib/rltk/lexers/calculator.rb +1 -1
- data/lib/rltk/lexers/ebnf.rb +8 -7
- data/lib/rltk/parser.rb +300 -247
- data/lib/rltk/parsers/infix_calc.rb +1 -1
- data/lib/rltk/parsers/postfix_calc.rb +2 -2
- data/lib/rltk/parsers/prefix_calc.rb +2 -2
- data/lib/rltk/token.rb +1 -2
- data/lib/rltk/version.rb +3 -3
- data/lib/rltk.rb +6 -6
- data/test/cg/tc_basic_block.rb +83 -0
- data/test/cg/tc_control_flow.rb +191 -0
- data/test/cg/tc_function.rb +54 -0
- data/test/cg/tc_generic_value.rb +33 -0
- data/test/cg/tc_instruction.rb +256 -0
- data/test/cg/tc_llvm.rb +25 -0
- data/test/cg/tc_math.rb +88 -0
- data/test/cg/tc_module.rb +89 -0
- data/test/cg/tc_transforms.rb +68 -0
- data/test/cg/tc_type.rb +69 -0
- data/test/cg/tc_value.rb +151 -0
- data/test/cg/ts_cg.rb +23 -0
- data/test/tc_ast.rb +105 -8
- data/test/tc_cfg.rb +63 -48
- data/test/tc_lexer.rb +84 -96
- data/test/tc_parser.rb +224 -52
- data/test/tc_token.rb +6 -6
- data/test/ts_rltk.rb +12 -15
- metadata +149 -75
- data/lib/rltk/cg/generated_extended_bindings.rb +0 -287
- data/lib/rltk/util/abstract_class.rb +0 -25
- data/lib/rltk/util/monkeys.rb +0 -129
data/lib/rltk/cg/basic_block.rb
CHANGED
@@ -16,7 +16,7 @@ require 'rltk/cg/value'
|
|
16
16
|
# Classes and Modules #
|
17
17
|
#######################
|
18
18
|
|
19
|
-
module RLTK::CG
|
19
|
+
module RLTK::CG
|
20
20
|
|
21
21
|
# A BasicBlock is what instructions are inserted into and what functions
|
22
22
|
# are made of. BasicBlock objects may be created either using
|
data/lib/rltk/cg/bindings.rb
CHANGED
@@ -3,16 +3,22 @@
|
|
3
3
|
# Date: 2012/03/08
|
4
4
|
# Description: This file holds bindings to LLVM.
|
5
5
|
|
6
|
+
#########
|
7
|
+
# Notes #
|
8
|
+
#########
|
9
|
+
|
10
|
+
# 1) initialize_all_target_m_cs -> initialize_all_target_mcs
|
11
|
+
# 2) initialize_obj_carc_opts -> initialize_objc_arc_opts
|
12
|
+
|
6
13
|
############
|
7
14
|
# Requires #
|
8
15
|
############
|
9
16
|
|
10
17
|
# Gems
|
11
|
-
require 'rubygems'
|
12
18
|
require 'ffi'
|
19
|
+
require 'filigree/boolean'
|
13
20
|
|
14
21
|
# Ruby Language Toolkit
|
15
|
-
require 'rltk/util/monkeys'
|
16
22
|
require 'rltk/version'
|
17
23
|
require 'rltk/cg'
|
18
24
|
|
@@ -20,7 +26,7 @@ require 'rltk/cg'
|
|
20
26
|
# Classes and Modules #
|
21
27
|
#######################
|
22
28
|
|
23
|
-
module RLTK::CG
|
29
|
+
module RLTK::CG
|
24
30
|
|
25
31
|
# This module provides access to stored FFI::Pointer objects and allows a
|
26
32
|
# class to be passed directly into FFI methods. It also provides a
|
@@ -52,22 +58,6 @@ module RLTK::CG # :nodoc:
|
|
52
58
|
# Require the generated bindings files while handling errors.
|
53
59
|
require 'rltk/cg/generated_bindings'
|
54
60
|
|
55
|
-
begin
|
56
|
-
require 'rltk/cg/generated_extended_bindings'
|
57
|
-
|
58
|
-
# Check to make sure that we have the same target version as the ECB.
|
59
|
-
if target_version() != RLTK::LLVM_TARGET_VERSION
|
60
|
-
raise LibraryMismatch,
|
61
|
-
"Extended bindings expected LLVM version #{target_version()}, " +
|
62
|
-
"RLTK expects LLVM version #{RLTK::LLVM_TARGET_VERSION}"
|
63
|
-
end
|
64
|
-
|
65
|
-
@ecb = true
|
66
|
-
|
67
|
-
rescue LoadError
|
68
|
-
@ecb = false
|
69
|
-
end
|
70
|
-
|
71
61
|
#############
|
72
62
|
# Constants #
|
73
63
|
#############
|
@@ -119,11 +109,6 @@ module RLTK::CG # :nodoc:
|
|
119
109
|
# Methods #
|
120
110
|
###########
|
121
111
|
|
122
|
-
# @return [Boolean] If the Extended C Bindings for LLVM are present.
|
123
|
-
def self.ecb?
|
124
|
-
@ecb
|
125
|
-
end
|
126
|
-
|
127
112
|
# Converts a CamelCase string into an underscored string.
|
128
113
|
#
|
129
114
|
# @param [#to_s] name CamelCase string.
|
@@ -162,7 +147,5 @@ module RLTK::CG # :nodoc:
|
|
162
147
|
ASM_PRINTERS.each do |asm|
|
163
148
|
add_binding("LLVMInitialize#{asm}AsmPrinter", [], :void)
|
164
149
|
end
|
165
|
-
|
166
|
-
add_binding(:LLVMDisposeMessage, [:pointer], :void)
|
167
150
|
end
|
168
151
|
end
|
data/lib/rltk/cg/builder.rb
CHANGED
@@ -15,7 +15,7 @@ require 'rltk/cg/instruction'
|
|
15
15
|
# Classes and Modules #
|
16
16
|
#######################
|
17
17
|
|
18
|
-
module RLTK::CG
|
18
|
+
module RLTK::CG
|
19
19
|
|
20
20
|
# This class is responsible for adding {Instruction Instructions} to {BasicBlock BasicBlocks}.
|
21
21
|
class Builder
|
@@ -67,7 +67,7 @@ module RLTK::CG # :nodoc:
|
|
67
67
|
#
|
68
68
|
# @return [Instruction] Build instruction.
|
69
69
|
def build_inst(inst, *args)
|
70
|
-
self.send(inst
|
70
|
+
self.send(inst, *args)
|
71
71
|
end
|
72
72
|
alias :'<<' :build_inst
|
73
73
|
|
@@ -91,6 +91,8 @@ module RLTK::CG # :nodoc:
|
|
91
91
|
Bindings.position_builder_at_end(@ptr, bb) if check_type(bb, BasicBlock, 'bb')
|
92
92
|
self
|
93
93
|
end
|
94
|
+
alias :pae :position_at_end
|
95
|
+
alias :target :position_at_end
|
94
96
|
|
95
97
|
# Position the Builder before the given Instruction.
|
96
98
|
#
|
@@ -261,7 +263,7 @@ module RLTK::CG # :nodoc:
|
|
261
263
|
#
|
262
264
|
# @return [PhiInst] The phi node.
|
263
265
|
def phi(type, incoming, name = '')
|
264
|
-
|
266
|
+
PhiInst.new(Bindings.build_phi(@ptr, check_cg_type(type), name)).tap do |phi|
|
265
267
|
phi.incoming.add(incoming)
|
266
268
|
end
|
267
269
|
end
|
@@ -302,7 +304,7 @@ module RLTK::CG # :nodoc:
|
|
302
304
|
#
|
303
305
|
# @return [SwitchInst]
|
304
306
|
def switch(val, default, cases)
|
305
|
-
|
307
|
+
SwitchInst.new(Bindings.build_switch(@ptr, val, default, cases.size)).tap do |inst|
|
306
308
|
cases.each { |val, block| inst.add_case(val, block) }
|
307
309
|
end
|
308
310
|
end
|
@@ -804,18 +806,48 @@ module RLTK::CG # :nodoc:
|
|
804
806
|
#
|
805
807
|
# @return [GlobalStringPtrInst] Reference to the global string pointer.
|
806
808
|
def gloabl_string_pointer(string, name = '')
|
807
|
-
GlobalStringPtrInst(Bindings.build_global_string_ptr(@ptr, string, name))
|
809
|
+
GlobalStringPtrInst.new(Bindings.build_global_string_ptr(@ptr, string, name))
|
810
|
+
end
|
811
|
+
|
812
|
+
#######################
|
813
|
+
# Atomic Instructions #
|
814
|
+
#######################
|
815
|
+
|
816
|
+
# Create an atomic read/modify/write instruction.
|
817
|
+
#
|
818
|
+
# @see http://llvm.org/docs/LangRef.html#atomic-memory-ordering-constraints
|
819
|
+
#
|
820
|
+
# @param [Symbol from _enum_atomic_rmw_bin_op_] op Operation to perform
|
821
|
+
# @param [OpaqueValue] addr Address to modify
|
822
|
+
# @param [OpaqueValue] val Value to test
|
823
|
+
# @param [Symbol from _enum_atomic_ordering_] ordering Memory ordering constraints
|
824
|
+
# @param [Boolean] single_thread Synchronize with single thread or all threads
|
825
|
+
#
|
826
|
+
# @return [AtomicRMWInst]
|
827
|
+
def atomic_rmw(op, addr, val, ordering, single_thread)
|
828
|
+
AtomicRMWInst.new(Bindings.build_atomic_rmw(@ptr, op, addr, val, ordering, single_thread.to_i))
|
808
829
|
end
|
809
830
|
|
810
831
|
###############################
|
811
832
|
# Type and Value Manipulation #
|
812
833
|
###############################
|
813
834
|
|
835
|
+
# Cast a value to a given address space.
|
836
|
+
#
|
837
|
+
# @param [Value] val Value to cast
|
838
|
+
# @param [Type] type Target type
|
839
|
+
# @param [String] name Name of the result in LLVM IR
|
840
|
+
#
|
841
|
+
# @return [AddrSpaceCastInst]
|
842
|
+
def addr_space_cast(val, type, name = '')
|
843
|
+
AddrSpaceCast.new(Bindings.addr_space_cast(@ptr, val, check_cg_type(type), name))
|
844
|
+
end
|
845
|
+
|
814
846
|
# Cast a value to the given type without changing any bits.
|
815
847
|
#
|
816
|
-
# @param [Value]
|
817
|
-
# @param [Type]
|
818
|
-
# @param [String]
|
848
|
+
# @param [Value] val Value to cast
|
849
|
+
# @param [Type] type Target type
|
850
|
+
# @param [String] name Name of the result in LLVM IR
|
819
851
|
#
|
820
852
|
# @return [BitCastInst] A value of the target type.
|
821
853
|
def bitcast(val, type, name = '')
|
data/lib/rltk/cg/context.rb
CHANGED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Author: Chris Wailes <chris.wailes@gmail.com>
|
2
|
+
# Project: Ruby Language Toolkit
|
3
|
+
# Date: 2012/09/21
|
4
|
+
# Description: This file contains a combination of the Visitor and Builder
|
5
|
+
# classes called a Contractor.
|
6
|
+
|
7
|
+
############
|
8
|
+
# Requires #
|
9
|
+
############
|
10
|
+
|
11
|
+
# Gems
|
12
|
+
require 'filigree/visitor'
|
13
|
+
|
14
|
+
# Ruby Language Toolkit
|
15
|
+
require 'rltk/cg/builder'
|
16
|
+
|
17
|
+
#######################
|
18
|
+
# Classes and Modules #
|
19
|
+
#######################
|
20
|
+
|
21
|
+
module RLTK::CG
|
22
|
+
|
23
|
+
class Contractor < Builder
|
24
|
+
|
25
|
+
include Filigree::Visitor
|
26
|
+
|
27
|
+
####################
|
28
|
+
# Instance Methods #
|
29
|
+
####################
|
30
|
+
|
31
|
+
# Alias out the RLTK::Visitor.visit method.
|
32
|
+
alias :wrapped_visit :visit
|
33
|
+
|
34
|
+
# Visit an object in the context of this builder. See the
|
35
|
+
# Filigree::Visitor's visit method for more details about the basic
|
36
|
+
# behaviour of this method. The special options for this method are:
|
37
|
+
#
|
38
|
+
# @param [Object] object The object to visit.
|
39
|
+
# @param [BasicBlock] at Where to position the contractor before visiting the object.
|
40
|
+
# @param [Boolean] rcb If specified the method will also return the block where the contractor is currently positioned.
|
41
|
+
#
|
42
|
+
# @return [Object]
|
43
|
+
def visit(object, at: nil, rcb: false)
|
44
|
+
target at if at
|
45
|
+
|
46
|
+
result = wrapped_visit(object)
|
47
|
+
|
48
|
+
if rcb then [result, current_block] else result end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -8,8 +8,10 @@
|
|
8
8
|
# Requires #
|
9
9
|
############
|
10
10
|
|
11
|
+
# Gems
|
12
|
+
require 'filigree/abstract_class'
|
13
|
+
|
11
14
|
# Ruby Language Toolkit
|
12
|
-
require 'rltk/util/abstract_class'
|
13
15
|
require 'rltk/cg/bindings'
|
14
16
|
require 'rltk/cg/pass_manager'
|
15
17
|
require 'rltk/cg/target'
|
@@ -18,7 +20,7 @@ require 'rltk/cg/target'
|
|
18
20
|
# Classes and Modules #
|
19
21
|
#######################
|
20
22
|
|
21
|
-
module RLTK::CG
|
23
|
+
module RLTK::CG
|
22
24
|
|
23
25
|
# The ExecutionEngine class and its subclasses execute code from the
|
24
26
|
# provided module, as well as providing a {PassManager} and
|
@@ -26,7 +28,7 @@ module RLTK::CG # :nodoc:
|
|
26
28
|
#
|
27
29
|
# @abstract Implemented by {Interpreter} and {JITCompiler}.
|
28
30
|
class ExecutionEngine
|
29
|
-
include AbstractClass
|
31
|
+
include Filigree::AbstractClass
|
30
32
|
include BindingClass
|
31
33
|
|
32
34
|
# The Proc object called by the garbage collector to free resources used by LLVM.
|
@@ -46,13 +48,13 @@ module RLTK::CG # :nodoc:
|
|
46
48
|
|
47
49
|
block = Proc.new { |ptr, error| Bindings.create_execution_engine_for_module(ptr, mod, error) } if block == nil
|
48
50
|
|
49
|
-
ptr
|
50
|
-
error
|
51
|
-
status
|
51
|
+
ptr = FFI::MemoryPointer.new(:pointer)
|
52
|
+
error = FFI::MemoryPointer.new(:pointer)
|
53
|
+
status = block.call(ptr, error)
|
52
54
|
|
53
55
|
if status.zero?
|
54
|
-
@ptr
|
55
|
-
@module
|
56
|
+
@ptr = ptr.read_pointer
|
57
|
+
@module = mod
|
56
58
|
|
57
59
|
# Associate this engine with the provided module.
|
58
60
|
@module.engine = self
|
@@ -154,4 +156,39 @@ module RLTK::CG # :nodoc:
|
|
154
156
|
end
|
155
157
|
end
|
156
158
|
end
|
159
|
+
|
160
|
+
# Options for initializing a {MCJITCompiler}.
|
161
|
+
class MCJITCompilerOptions < RLTK::CG::Bindings::MCJITCompilerOptions
|
162
|
+
|
163
|
+
# Create an object representing MCJIT compiler options.
|
164
|
+
#
|
165
|
+
# @param [Integer] opt_level Optimization level
|
166
|
+
# @param [Symbol from _enum_code_model_] code_model JIT compilation code model
|
167
|
+
# @param [Boolean] no_frame_pointer_elim Disable frame pointer elimination
|
168
|
+
# @param [Boolean] enable_fast_i_sel Turn on fast instruction selection
|
169
|
+
def initialize(opt_level = 0, code_model = :jit_default, no_frame_pointer_elim = false,
|
170
|
+
enable_fast_i_sel = true)
|
171
|
+
|
172
|
+
Bindings.initialize_mcjit_compiler_options(self.to_ptr, self.class.size)
|
173
|
+
|
174
|
+
super(opt_level, code_model, no_frame_pointer_elim.to_i, enable_fast_i_sel.to_i, nil)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
# The new LLVM JIT execution engine.
|
179
|
+
class MCJITCompiler < ExecutionEngine
|
180
|
+
|
181
|
+
# Create a new MC just-in-time-compiler.
|
182
|
+
#
|
183
|
+
# @see http://llvm.org/docs/MCJITDesignAndImplementation.html
|
184
|
+
# @see http://blog.llvm.org/2013/07/using-mcjit-with-kaleidoscope-tutorial.html
|
185
|
+
#
|
186
|
+
# @param [Module] mod Module to be executed
|
187
|
+
# @param [MCJITCompilerOptions] options Options used to create the MCJIT
|
188
|
+
def initialize(mod, options = MCJITCompilerOptions.new)
|
189
|
+
super(mod) do |ptr, error|
|
190
|
+
Bindings.create_mcjit_compiler_for_module(ptr, mod, options, MCJITCompilerOptions.size, error)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
157
194
|
end
|
data/lib/rltk/cg/function.rb
CHANGED
@@ -16,7 +16,7 @@ require 'rltk/cg/value'
|
|
16
16
|
# Classes and Modules #
|
17
17
|
#######################
|
18
18
|
|
19
|
-
module RLTK::CG
|
19
|
+
module RLTK::CG
|
20
20
|
|
21
21
|
# An LLVM IR function.
|
22
22
|
class Function < GlobalValue
|
@@ -131,7 +131,7 @@ module RLTK::CG # :nodoc:
|
|
131
131
|
#
|
132
132
|
# @return [BasicBlock] New BasicBlock.
|
133
133
|
def append(name = '', builder = nil, context = nil, *block_args, &block)
|
134
|
-
BasicBlock.new(@fun, name, builder, context,
|
134
|
+
BasicBlock.new(@fun, name, builder, context, *block_args, &block)
|
135
135
|
end
|
136
136
|
|
137
137
|
# An iterator for each block inside this collection.
|
@@ -175,6 +175,16 @@ module RLTK::CG # :nodoc:
|
|
175
175
|
class FunctionAttrCollection < AttrCollection
|
176
176
|
@@add_method = :add_function_attr
|
177
177
|
@@del_method = :remove_function_attr
|
178
|
+
|
179
|
+
# Set a target-dependent function attribute.
|
180
|
+
#
|
181
|
+
# @param [String] attribute Attribute name
|
182
|
+
# @param [String] value Attribute value
|
183
|
+
#
|
184
|
+
# @return [void]
|
185
|
+
def add_td_attr(attribute, value)
|
186
|
+
Bindings.add_target_dependent_function_attr(@value, attribute, value)
|
187
|
+
end
|
178
188
|
end
|
179
189
|
|
180
190
|
|