rggen-core 0.19.0 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/exe/rggen +0 -0
  3. data/lib/rggen/core.rb +4 -3
  4. data/lib/rggen/core/base/component.rb +16 -7
  5. data/lib/rggen/core/base/component_factory.rb +12 -9
  6. data/lib/rggen/core/base/component_layer_extension.rb +194 -0
  7. data/lib/rggen/core/base/feature_layer_extension.rb +158 -0
  8. data/lib/rggen/core/builder/builder.rb +25 -25
  9. data/lib/rggen/core/builder/component_entry.rb +3 -2
  10. data/lib/rggen/core/builder/component_registry.rb +22 -17
  11. data/lib/rggen/core/builder/{category.rb → layer.rb} +27 -26
  12. data/lib/rggen/core/builder/plugins.rb +5 -3
  13. data/lib/rggen/core/configuration/component.rb +5 -0
  14. data/lib/rggen/core/configuration/component_factory.rb +1 -1
  15. data/lib/rggen/core/configuration/input_data.rb +15 -0
  16. data/lib/rggen/core/input_base/component_factory.rb +37 -6
  17. data/lib/rggen/core/input_base/feature.rb +3 -3
  18. data/lib/rggen/core/input_base/input_data.rb +13 -10
  19. data/lib/rggen/core/input_base/loader.rb +2 -3
  20. data/lib/rggen/core/input_base/yaml_loader.rb +5 -4
  21. data/lib/rggen/core/output_base/code_generator.rb +10 -23
  22. data/lib/rggen/core/output_base/component.rb +42 -39
  23. data/lib/rggen/core/output_base/component_factory.rb +4 -0
  24. data/lib/rggen/core/output_base/feature.rb +6 -7
  25. data/lib/rggen/core/register_map.rb +3 -3
  26. data/lib/rggen/core/register_map/component.rb +4 -3
  27. data/lib/rggen/core/register_map/component_factory.rb +18 -1
  28. data/lib/rggen/core/register_map/feature.rb +2 -2
  29. data/lib/rggen/core/register_map/hash_loader.rb +16 -23
  30. data/lib/rggen/core/register_map/input_data.rb +30 -16
  31. data/lib/rggen/core/register_map/loader.rb +1 -1
  32. data/lib/rggen/core/register_map/ruby_loader.rb +1 -1
  33. data/lib/rggen/core/utility/code_utility.rb +8 -2
  34. data/lib/rggen/core/version.rb +1 -1
  35. metadata +7 -6
  36. data/lib/rggen/core/base/hierarchical_accessors.rb +0 -91
  37. data/lib/rggen/core/base/hierarchical_feature_accessors.rb +0 -83
@@ -25,9 +25,8 @@ module RgGen
25
25
  end
26
26
 
27
27
  def load_file(file)
28
- File.readable?(file) || (
29
- raise Core::LoadError.new('cannot load such file', file)
30
- )
28
+ File.readable?(file) ||
29
+ (raise Core::LoadError.new('cannot load such file', file))
31
30
  format(read_file(file), file)
32
31
  end
33
32
 
@@ -26,13 +26,14 @@ module RgGen
26
26
 
27
27
  def symbolize_keys(result)
28
28
  if result.is_a? Hash
29
- result.keys.each do |key|
30
- result[key.to_sym] = symbolize_keys(result.delete(key))
29
+ result.each_with_object({}) do |(key, value), hash|
30
+ hash[key.to_sym] = symbolize_keys(value)
31
31
  end
32
32
  elsif result.is_a? Array
33
- result.map! { |value| symbolize_keys(value) }
33
+ result.map(&method(:symbolize_keys))
34
+ else
35
+ result
34
36
  end
35
- result
36
37
  end
37
38
  end
38
39
 
@@ -9,11 +9,10 @@ module RgGen
9
9
  code_blocks[kind] << block
10
10
  end
11
11
 
12
- def generate(context, kind, code)
13
- return code unless generatable?(kind)
14
- execute_code_blocks(
15
- context, kind, code || context.create_blank_code
16
- )
12
+ def generate(context, code, kind)
13
+ code_blocks[kind].each do |block|
14
+ execute_code_block(context, code, block)
15
+ end
17
16
  end
18
17
 
19
18
  def copy
@@ -28,30 +27,18 @@ module RgGen
28
27
  @code_blocks ||= Hash.new { |blocks, kind| blocks[kind] = [] }
29
28
  end
30
29
 
31
- def generatable?(kind)
32
- @code_blocks&.key?(kind)
33
- end
34
-
35
- def execute_code_blocks(context, kind, code)
36
- code_blocks[kind].each(&code_block_executor(context, code))
37
- code
38
- end
39
-
40
- def code_block_executor(context, code)
41
- lambda do |block|
42
- if block.arity.zero?
43
- code << context.instance_exec(&block)
44
- else
45
- context.instance_exec(code, &block)
46
- end
30
+ def execute_code_block(context, code, block)
31
+ if block.arity.zero?
32
+ code << context.instance_exec(&block)
33
+ else
34
+ context .instance_exec(code, &block)
47
35
  end
48
36
  end
49
37
 
50
38
  protected
51
39
 
52
40
  def copy_code_blocks(original_blocks)
53
- original_blocks
54
- .each { |kind, blocks| code_blocks[kind] = blocks.dup }
41
+ original_blocks.each { |kind, blocks| code_blocks[kind] = blocks.dup }
55
42
  end
56
43
  end
57
44
  end
@@ -4,22 +4,21 @@ module RgGen
4
4
  module Core
5
5
  module OutputBase
6
6
  class Component < Base::Component
7
- include Base::HierarchicalAccessors
7
+ include Base::ComponentLayerExtension
8
8
 
9
9
  attr_reader :configuration
10
- attr_reader :source
10
+ attr_reader :register_map
11
11
 
12
- def post_initialize(_paren, configuration, source)
12
+ def post_initialize(configuration, register_map)
13
13
  @configuration = configuration
14
- @source = source
15
- @need_children = source.need_children?
16
- define_hierarchical_accessors
17
- define_children_presense_indicator
18
- define_proxy_calls(@source, @source.properties)
14
+ @register_map = register_map
15
+ @need_children = register_map.need_children?
16
+ define_layer_methods
17
+ define_proxy_calls(@register_map, @register_map.properties)
19
18
  end
20
19
 
21
20
  def children?
22
- !source.children.empty?
21
+ !register_map.children.empty?
23
22
  end
24
23
 
25
24
  def add_feature(feature)
@@ -28,7 +27,7 @@ module RgGen
28
27
  end
29
28
 
30
29
  def printables
31
- @source.printables
30
+ register_map.printables
32
31
  end
33
32
 
34
33
  def pre_build
@@ -40,8 +39,9 @@ module RgGen
40
39
  @children.each(&:build)
41
40
  end
42
41
 
43
- def generate_code(kind, mode, code = nil)
44
- code_generators(kind, mode).inject(code) { |c, g| g[c] }
42
+ def generate_code(code, kind, mode, target_or_range = nil, depth = 0)
43
+ code_generator_contexts(kind, mode, target_or_range, depth)
44
+ .each { |context| context.generate(code) }
45
45
  end
46
46
 
47
47
  def write_file(directory = nil)
@@ -51,18 +51,6 @@ module RgGen
51
51
 
52
52
  private
53
53
 
54
- INDICATOR_NAMES = {
55
- register_map: :register_blocks?,
56
- register_block: :registers?,
57
- register: :bit_fields?
58
- }.freeze
59
-
60
- def define_children_presense_indicator
61
- indicator_name = INDICATOR_NAMES[hierarchy]
62
- indicator_name &&
63
- singleton_exec { alias_method indicator_name, :children? }
64
- end
65
-
66
54
  def build_feature(feature)
67
55
  feature.build
68
56
  import_feature_methods(feature, :object)
@@ -73,25 +61,40 @@ module RgGen
73
61
  define_proxy_calls(feature, methods)
74
62
  end
75
63
 
76
- def code_generators(kind, mode)
64
+ CodeGeneratorContext = Struct.new(:receivers, :args) do
65
+ def generate(code)
66
+ receivers.each { |receiver| receiver.generate_code(code, *args) }
67
+ end
68
+ end
69
+
70
+ def code_generator_contexts(kind, mode, target_or_range, depth)
77
71
  [
78
- [@features.each_value, [:pre_code, kind]],
79
- *main_code_contexts(kind, mode),
80
- [@features.each_value, [:post_code, kind]]
81
- ].map do |receivers, args|
82
- lambda do |code|
83
- receivers.inject(code) { |c, r| r.generate_code(*args, c) }
84
- end
72
+ feature_code_generator_context(:pre_code, kind, target_or_range, depth),
73
+ *main_code_generator_contexts(kind, mode, target_or_range, depth),
74
+ feature_code_generator_context(:post_code, kind, target_or_range, depth)
75
+ ].compact
76
+ end
77
+
78
+ def feature_code_generator_context(phase, kind, target_or_range, depth)
79
+ (target_depth?(depth, target_or_range) || nil) &&
80
+ CodeGeneratorContext.new(@features.each_value, [phase, kind])
81
+ end
82
+
83
+ def target_depth?(depth, target_or_range)
84
+ if target_or_range.nil?
85
+ true
86
+ elsif target_or_range.respond_to?(:include?)
87
+ target_or_range.include?(depth)
88
+ else
89
+ depth == target_or_range
85
90
  end
86
91
  end
87
92
 
88
- def main_code_contexts(kind, mode)
89
- contexts = [
90
- [@features.each_value, [:main_code, kind]],
91
- [@children, [kind, mode]]
92
- ]
93
- contexts.reverse! if mode == :bottom_up
94
- contexts
93
+ def main_code_generator_contexts(kind, mode, target_or_range, depth)
94
+ [
95
+ feature_code_generator_context(:main_code, kind, target_or_range, depth),
96
+ CodeGeneratorContext.new(@children, [kind, mode, target_or_range, depth + 1])
97
+ ].tap { |contexts| mode == :bottom_up && contexts.reverse! }
95
98
  end
96
99
  end
97
100
  end
@@ -22,6 +22,10 @@ module RgGen
22
22
  end
23
23
  end
24
24
 
25
+ def find_child_factory(_configuration, register_map)
26
+ component_factories[register_map.layer]
27
+ end
28
+
25
29
  def post_build(component)
26
30
  component.pre_build
27
31
  end
@@ -4,7 +4,7 @@ module RgGen
4
4
  module Core
5
5
  module OutputBase
6
6
  class Feature < Base::Feature
7
- include Base::HierarchicalFeatureAccessors
7
+ include Base::FeatureLayerExtension
8
8
  include RaiseError
9
9
 
10
10
  class << self
@@ -64,9 +64,8 @@ module RgGen
64
64
 
65
65
  def export(*methods)
66
66
  methods.each do |method|
67
- exported_methods.include?(method) || (
68
- exported_methods << method
69
- )
67
+ exported_methods.include?(method) ||
68
+ (exported_methods << method)
70
69
  end
71
70
  end
72
71
  end
@@ -90,7 +89,7 @@ module RgGen
90
89
  end
91
90
 
92
91
  def post_initialize
93
- define_hierarchical_accessors
92
+ define_layer_methods
94
93
  end
95
94
 
96
95
  def pre_build
@@ -122,9 +121,9 @@ module RgGen
122
121
  end
123
122
  end
124
123
 
125
- def generate_code(phase, kind, code = nil)
124
+ def generate_code(code, phase, kind)
126
125
  generator = self.class.code_generators[phase]
127
- (generator&.generate(self, kind, code)) || code
126
+ generator&.generate(self, code, kind)
128
127
  end
129
128
 
130
129
  def write_file(directory = nil)
@@ -6,10 +6,10 @@ module RgGen
6
6
  def self.setup(builder)
7
7
  builder.input_component_registry(:register_map) do
8
8
  register_component [
9
- :register_map, :register_block, :register, :bit_field
10
- ] do |category|
9
+ :root, :register_block, :register_file, :register, :bit_field
10
+ ] do |layer|
11
11
  component Component, ComponentFactory
12
- feature Feature, FeatureFactory if category != :register_map
12
+ feature Feature, FeatureFactory if layer != :root
13
13
  end
14
14
 
15
15
  base_loader Loader
@@ -4,15 +4,16 @@ module RgGen
4
4
  module Core
5
5
  module RegisterMap
6
6
  class Component < InputBase::Component
7
- include Base::HierarchicalAccessors
7
+ include Base::ComponentLayerExtension
8
8
 
9
9
  attr_reader :configuration
10
10
 
11
11
  private
12
12
 
13
- def post_initialize(_parent, configuration)
13
+ def post_initialize(configuration)
14
14
  @configuration = configuration
15
- define_hierarchical_accessors
15
+ define_layer_methods
16
+ layer == :bit_field && need_no_children
16
17
  end
17
18
  end
18
19
  end
@@ -4,6 +4,8 @@ module RgGen
4
4
  module Core
5
5
  module RegisterMap
6
6
  class ComponentFactory < InputBase::ComponentFactory
7
+ include RaiseError
8
+
7
9
  private
8
10
 
9
11
  def select_actual_sources(configuration, *_)
@@ -11,7 +13,22 @@ module RgGen
11
13
  end
12
14
 
13
15
  def create_input_data(&block)
14
- RegisterMapData.new(valid_value_lists, &block)
16
+ InputData.new(:root, valid_value_lists, &block)
17
+ end
18
+
19
+ def find_child_factory(_configuration, register_map)
20
+ component_factories[register_map.layer]
21
+ end
22
+
23
+ NO_CHILDREN_ERROR_MESSAGES = {
24
+ root: 'no register blocks are given',
25
+ register_block: 'neither register files nor registers are given',
26
+ register_file: 'neither register files nor registers are given',
27
+ register: 'no bit fields are given'
28
+ }.freeze
29
+
30
+ def raise_no_children_error(comoponent)
31
+ error(NO_CHILDREN_ERROR_MESSAGES[comoponent.layer])
15
32
  end
16
33
  end
17
34
  end
@@ -4,7 +4,7 @@ module RgGen
4
4
  module Core
5
5
  module RegisterMap
6
6
  class Feature < InputBase::Feature
7
- include Base::HierarchicalFeatureAccessors
7
+ include Base::FeatureLayerExtension
8
8
  include RaiseError
9
9
 
10
10
  private
@@ -14,7 +14,7 @@ module RgGen
14
14
  end
15
15
 
16
16
  def post_initialize
17
- define_hierarchical_accessors
17
+ define_layer_methods
18
18
  end
19
19
  end
20
20
  end
@@ -5,41 +5,34 @@ module RgGen
5
5
  module RegisterMap
6
6
  module HashLoader
7
7
  def format(read_data, file)
8
- format_data(:register_map, register_map, read_data, file)
8
+ format_data(:root, root, read_data, file)
9
9
  rescue TypeError => e
10
10
  raise Core::LoadError.new(e.message, file)
11
11
  end
12
12
 
13
13
  private
14
14
 
15
- CHILD_HIERARCHY = {
16
- register_map: :register_block,
17
- register_block: :register,
18
- register: :bit_field
15
+ LAYER_MAP = {
16
+ root: { register_blocks: :register_block },
17
+ register_block: { register_files: :register_file, registers: :register },
18
+ register_file: { register_files: :register_file, registers: :register },
19
+ register: { bit_fields: :bit_field }
19
20
  }.freeze
20
21
 
21
- CHILD_HIERARCHY_KEY = {
22
- register_map: :register_blocks,
23
- register_block: :registers,
24
- register: :bit_fields
25
- }.freeze
26
-
27
- def format_data(hierarchy, input_data, read_data, file)
22
+ def format_data(layer, input_data, read_data, file)
28
23
  read_data = Hash(read_data)
29
24
  input_data.values(read_data, file)
30
- read_data_for_child(hierarchy, read_data).each do |data|
31
- format_data(
32
- CHILD_HIERARCHY[hierarchy],
33
- input_data.child,
34
- data,
35
- file
36
- )
37
- end
25
+ format_next_layer_data(layer, input_data, read_data, file)
38
26
  end
39
27
 
40
- def read_data_for_child(hierarchy, read_data)
41
- key = CHILD_HIERARCHY_KEY[hierarchy]
42
- Array(key && read_data[key])
28
+ def format_next_layer_data(layer, input_data, read_data, file)
29
+ LAYER_MAP[layer]&.each do |key, next_layer|
30
+ Array(read_data[key]).each do |data|
31
+ format_data(
32
+ next_layer, input_data.child(next_layer), data, file
33
+ )
34
+ end
35
+ end
43
36
  end
44
37
  end
45
38
  end
@@ -3,30 +3,44 @@
3
3
  module RgGen
4
4
  module Core
5
5
  module RegisterMap
6
- class BitFieldData < InputBase::InputData
7
- end
6
+ class InputData < InputBase::InputData
7
+ module Root
8
+ def register_block(value_list = nil, &block)
9
+ child(:register_block, value_list, &block)
10
+ end
11
+ end
8
12
 
9
- class RegisterData < InputBase::InputData
10
- alias_method :bit_field, :child
13
+ module RegisterBlockRegisterFile
14
+ def register_file(value_list = nil, &block)
15
+ child(:register_file, value_list, &block)
16
+ end
11
17
 
12
- def child_data_class
13
- BitFieldData
18
+ def register(value_list = nil, &block)
19
+ child(:register, value_list, &block)
20
+ end
14
21
  end
15
- end
16
22
 
17
- class RegisterBlockData < InputBase::InputData
18
- alias_method :register, :child
23
+ module Register
24
+ def bit_field(value_list = nil, &block)
25
+ child(:bit_field, value_list, &block)
26
+ end
27
+ end
19
28
 
20
- def child_data_class
21
- RegisterData
29
+ module BitField
30
+ def self.extended(object)
31
+ object.singleton_exec { undef_method :child }
32
+ end
22
33
  end
23
- end
24
34
 
25
- class RegisterMapData < InputBase::InputData
26
- alias_method :register_block, :child
35
+ LAYER_EXTENSIONS = {
36
+ root: Root, register_block: RegisterBlockRegisterFile,
37
+ register_file: RegisterBlockRegisterFile, register: Register,
38
+ bit_field: BitField
39
+ }.freeze
27
40
 
28
- def child_data_class
29
- RegisterBlockData
41
+ def initialize(layer, valid_value_list)
42
+ extend(LAYER_EXTENSIONS[layer])
43
+ super
30
44
  end
31
45
  end
32
46
  end