Class: HDLRuby::Low::Scope

Inherits:
Object
  • Object
show all
Includes:
ForceName, Hparent, Low2Symbol
Defined in:
lib/HDLRuby/hruby_low.rb,
lib/HDLRuby/hruby_low2c.rb,
lib/HDLRuby/hruby_low2hdr.rb,
lib/HDLRuby/hruby_low2seq.rb,
lib/HDLRuby/hruby_low2sym.rb,
lib/HDLRuby/hruby_low2vhd.rb,
lib/HDLRuby/hruby_low2high.rb,
lib/HDLRuby/hruby_low_cleanup.rb,
lib/HDLRuby/hruby_low_mutable.rb,
lib/HDLRuby/hruby_low_resolve.rb,
lib/HDLRuby/hruby_low_skeleton.rb,
lib/HDLRuby/hruby_low_fix_types.rb,
lib/HDLRuby/hruby_low_with_port.rb,
lib/HDLRuby/hruby_low_bool2select.rb,
lib/HDLRuby/hruby_low_without_concat.rb,
lib/HDLRuby/hruby_low_without_select.rb,
lib/HDLRuby/backend/hruby_c_allocator.rb,
lib/HDLRuby/hruby_low_without_parinseq.rb,
lib/HDLRuby/hruby_low_without_namespace.rb,
lib/HDLRuby/hruby_low_casts_without_expression.rb

Overview

Extends the Scope class with functionality for extracting expressions from cast.

Direct Known Subclasses

High::Scope

Constant Summary

Constants included from Low2Symbol

Low2Symbol::Low2SymbolPrefix, Low2Symbol::Low2SymbolTable, Low2Symbol::Symbol2LowTable

Instance Attribute Summary collapse

Attributes included from Hparent

#parent

Instance Method Summary collapse

Methods included from ForceName

#extend_name!, #force_name!

Methods included from Low2Symbol

#to_sym

Methods included from Hparent

#hierarchy, #no_parent!, #scope

Constructor Details

#initialize(name = :"") ⇒ Scope

Creates a new scope with a possible +name+.



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/HDLRuby/hruby_low.rb', line 516

def initialize(name = :"")
    # Check and set the name.
    @name = name.to_sym
    # Initialize the local types.
    @types = HashName.new
    # Initialize the local system types.
    @systemTs = HashName.new
    # Initialize the sub scopes.
    @scopes = []
    # Initialize the inner signal instance lists.
    @inners  = HashName.new
    # Initialize the system instances list.
    @systemIs = HashName.new
    # Initialize the non-HDLRuby code chunks list.
    @codes = []
    # Initialize the connections list.
    @connections = []
    # Initialize the behaviors lists.
    @behaviors = []
end

Instance Attribute Details

#nameObject (readonly)

The name of the scope if any



513
514
515
# File 'lib/HDLRuby/hruby_low.rb', line 513

def name
  @name
end

Instance Method Details

#add_behavior(behavior) ⇒ Object

Adds a +behavior+.



1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
# File 'lib/HDLRuby/hruby_low.rb', line 1041

def add_behavior(behavior)
    unless behavior.is_a?(Behavior)
        raise AnyError,"Invalid class for a behavior: #{behavior.class}"
    end
    # Set its parent
    behavior.parent = self
    # And add it
    @behaviors << behavior
    behavior
end

#add_code(code) ⇒ Object

Adds code chunk +code+.



811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
# File 'lib/HDLRuby/hruby_low.rb', line 811

def add_code(code)
    # Check and add the code chunk.
    unless code.is_a?(Code)
        raise AnyError,
              "Invalid class for a non-hDLRuby code chunk: #{code.class}"
    end
    # if @codes.include?(code) then
    #     raise AnyError, "Code #{code.name} already present."
    # end
    # Set the parent of the code chunk.
    code.parent = self
    # puts "code = #{code}, parent=#{self}"
    # Add the code chunk.
    @codes << code
    code
end

#add_connection(connection) ⇒ Object

Adds a +connection+.



986
987
988
989
990
991
992
993
994
995
996
# File 'lib/HDLRuby/hruby_low.rb', line 986

def add_connection(connection)
    unless connection.is_a?(Connection)
        raise AnyError,
              "Invalid class for a connection: #{connection.class}"
    end
    # Set the parent of the connection.
    connection.parent = self
    # And add it.
    @connections << connection
    connection
end

#add_inner(signal) ⇒ Object

Adds inner signal +signal+.



852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
# File 'lib/HDLRuby/hruby_low.rb', line 852

def add_inner(signal)
    # Check and add the signal.
    unless signal.is_a?(SignalI)
        raise AnyError,
              "Invalid class for a signal instance: #{signal.class}"
    end
    # if @inners.include?(signal) then
    #     raise AnyError, "SignalI #{signal.name} already present."
    # end
    # Set the parent of the signal.
    signal.parent = self
    # And add the signal.
    @inners.add(signal)
    return signal
end

#add_scope(scope) ⇒ Object

Adds a new +scope+.



702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# File 'lib/HDLRuby/hruby_low.rb', line 702

def add_scope(scope)
    # Check and add the scope.
    unless scope.is_a?(Scope)
        raise AnyError,
              "Invalid class for a system instance: #{scope.class}"
    end
    # if @scopes.include?(scope) then
    #     raise AnyError, "Scope #{scope} already present."
    # end
    # Set the parent of the scope
    scope.parent = self
    # Remove a former scope with same name if present (override)
    @scopes.delete_if { |sc| sc.name && sc.name == scope.name }
    # Add the scope
    @scopes << scope
end

#add_systemI(systemI) ⇒ Object

Adds system instance +systemI+.



759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# File 'lib/HDLRuby/hruby_low.rb', line 759

def add_systemI(systemI)
    # puts "add_systemI with name #{systemI.name}"
    # Check and add the systemI.
    unless systemI.is_a?(SystemI)
        raise AnyError,
              "Invalid class for a system instance: #{systemI.class}"
    end
    # if @systemIs.include?(systemI) then
    #     raise AnyError, "SystemI #{systemI.name} already present."
    # end
    # Set the parent of the instance
    systemI.parent = self
    # puts "systemI = #{systemI}, parent=#{self}"
    # Add the instance
    @systemIs.add(systemI)
end

#add_systemT(systemT) ⇒ Object

Adds system instance +systemT+.



596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/HDLRuby/hruby_low.rb', line 596

def add_systemT(systemT)
    # puts "add_systemT with name #{systemT.name}"
    # Check and add the systemT.
    unless systemT.is_a?(SystemT)
        raise AnyError,
              "Invalid class for a system type: #{systemT.class}"
    end
    # if @systemTs.include?(systemT) then
    #     raise AnyError, "SystemT #{systemT.name} already present."
    # end
    # Set the parent of the instance
    systemT.parent = self
    # puts "systemT = #{systemT}, parent=#{self}"
    # Add the instance
    @systemTs.add(systemT)
end

#add_type(type) ⇒ Object

Adds system instance +type+.



648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/HDLRuby/hruby_low.rb', line 648

def add_type(type)
    # puts "add_type with name #{type.name}"
    # Check and add the type.
    unless type.is_a?(Type)
        raise AnyError,
              "Invalid class for a type: #{type.class}"
    end
    # if @types.include?(type) then
    #     raise AnyError, "Type #{type.name} already present."
    # end
    # Set the parent of the instance
    type.parent = self
    # puts "type = #{type}, parent=#{self}"
    # Add the instance
    @types.add(type)
end

#blocks2seq!Object

Converts the par sub blocks to seq.



41
42
43
44
45
# File 'lib/HDLRuby/hruby_low2seq.rb', line 41

def blocks2seq!
    # Recurse on the behaviors.
    self.each_behavior { |beh| beh.blocks2seq! }
    return self
end

#boolean_in_assign2select!Object

Converts booleans in assignments to select operators.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/HDLRuby/hruby_low_bool2select.rb', line 35

def boolean_in_assign2select!
    # Recurse on the sub scopes.
    self.each_scope(&:boolean_in_assign2select!)

    # Apply on the connections.
    self.each_connection(&:boolean_in_assign2select!)

    # Apply on the behaviors.
    self.each_behavior do |behavior|
        behavior.block.boolean_in_assign2select!
    end
    return self
end

#break_concat_assigns!Object

Breaks the assignments to concats.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/HDLRuby/hruby_low_without_concat.rb', line 40

def break_concat_assigns!
    # Recruse on the sub scopes.
    self.each_scope(&:break_concat_assigns!)
    # Recurse on the statements.
    self.each_behavior do |behavior|
        behavior.block.each_block_deep(&:break_concat_assigns!)
    end
    # Work on the connections.
    self.each_connection.to_a.each do |connection|
        nconnection = connection.break_concat_assigns
        if nconnection.is_a?(Block) then
            # The connection has been broken, remove the former
            # version and add the generated block as a behavior.
            # self.remove_connection(connection)
            self.delete_connection!(connection)
            self.add_behavior(Behavior.new(nconnection))
        end
    end
end

#break_types!Object

Breaks the hierarchical types into sequences of type definitions. Assumes to_upper_space! has been called before.



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 255

def break_types!
    # The created types by structure.
    types = {}
    # Break the local types.
    self.each_type {|type| type.break_types!(types)}
    # Break the types in the inners.
    # self.each_inner {|inner| inner.type.break_types!(types) }
    self.each_inner do |inner|
        inner.set_type!(inner.type.break_types!(types))
    end
    # Break the types in the connections.
    self.each_connection do |connection| 
        connection.left.break_types!(types)
        connection.right.break_types!(types)
    end
    # Break the types in the behaviors.
    self.each_behavior do |behavior|
        behavior.each_event do |event|
            event.ref.break_types!(types) 
        end
        behavior.block.break_types!(types)
    end

    # Add the resulting types.
    types.each_value {|type| self.add_type(type) }
end

#c_code_allocate(allocator) ⇒ Object

Allocates signals within C code using +allocator+.



28
29
30
31
32
33
# File 'lib/HDLRuby/backend/hruby_c_allocator.rb', line 28

def c_code_allocate(allocator)
    # Interrate on the sub scopes.
    self.each_scope { |scope| scope.c_code_allocate(allocator) }
    # Ally thr allocator on the codes.
    self.each_code  { |code|  code.c_code_allocate(allocator) }
end

#casts_without_expression!Object

Extracts the expressions from the casts.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/HDLRuby/hruby_low_casts_without_expression.rb', line 36

def casts_without_expression!
    # Recurse on the sub scopes.
    self.each_scope(&:casts_without_expression!)

    # Apply on the connections.
    self.each_connection(&:casts_without_expression!)

    # Apply on the behaviors.
    self.each_behavior do |behavior|
        behavior.block.casts_without_expression!
    end
    return self
end

#cleanup!(keep) ⇒ Object

Cleans up. +keep+ includes the list of names to be kept.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/HDLRuby/hruby_low_cleanup.rb', line 38

def cleanup!(keep)
    # Complete the list of signals to keep with the signals parts
    # of the right values of connections and statements or
    # instance interface.
    self.each_scope_deep do |scope|
        # Connections.
        scope.each_connection do |connection|
            connection.right.each_node_deep do |node|
                # Leaf right value references are to keep.
                # They are either signal of current system or
                # system instance names.
                if node.is_a?(RefName) && !node.ref.is_a?(RefName) then
                    keep << node.name 
                end
            end
        end
        # System instances.
        scope.each_systemI do |systemI|
            keep << systemI.name
        end
        # Behaviors.
        scope.each_behavior do |behavior|
            behavior.block.each_node_deep do |node|
                # Skip left values.
                next if node.respond_to?(:leftvalue?) && node.leftvalue?
                # Leaf right value references are to keep.
                # They are either signal of current system or
                # system instance names.
                if node.is_a?(RefName) && !node.ref.is_a?(RefName) then
                    keep << node.name 
                end
            end
        end
    end
    
    # Remove the signals and correspondong assignments that are not 
    # to keep.
    self.delete_unless!(keep)
end

#delete_all_behaviors!Object

Deletes all the behaviors.



241
242
243
244
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 241

def delete_all_behaviors!
    @behaviors.each { |beh| beh.parent = nil }
    @behaviors = []
end

#delete_all_connections!Object

Deletes all the connections.



225
226
227
228
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 225

def delete_all_connections!
    @connections.each { |cnx| cnx.parent = nil }
    @connections = []
end

#delete_behavior!(behavior) ⇒ Object

Deletes a behavior.



231
232
233
234
235
236
237
238
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 231

def delete_behavior!(behavior)
    if @behaviors.include?(behavior) then
        # The behavior is present, delete it.
        @behaviors.delete(behavior)
        # And remove its parent.
        behavior.parent = nil
    end
end

#delete_connection!(connection) ⇒ Object

Deletes a connection.



214
215
216
217
218
219
220
221
222
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 214

def delete_connection!(connection)
    if @connections.include?(connection) then
        # The connection is present, delete it.
        @connections.delete(connection)
        # And remove its parent.
        connection.parent = nil
    end
    connection
end

#delete_inner!(signal) ⇒ Object

Deletes an inner.



192
193
194
195
196
197
198
199
200
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 192

def delete_inner!(signal)
    if @inners.key?(signal.name) then
        # The signal is present, delete it. 
        @inners.delete(signal.name)
        # And remove its parent.
        signal.parent = nil
    end
    signal
end

#delete_related!(*names) ⇒ Object

Deletes the elements related to one of +names+: either they have one of the names or they use an element with these names. NOTE: only delete actual instantiated elements, types or systemTs are left as is.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 250

def delete_related!(*names)
    # Delete the sub scopes whose name are in names.
    @scopes.delete_if { |scope| names.include?(scope.name) }
    # Delete the inner signals whose name are in names.
    @inners.delete_if { |sig| names.include?(sig.name) }
    # Delete the connections that contain signals whose name are
    # in names.
    @connections.delete_if { |connection| connection.use_name?(*names) }
    # Delete the behaviors whose block name or events' name are in
    # names.
    @behaviors.delete_if do |behavior|
        names.include?(behavior.block.name) or
        behavior.each_event.include? do |event|
            event.ref.use_name?(*names)
        end
    end
    
    # Recurse on the sub scopes.
    @scopes.each { |scope| scope.delete_related!(names) }
    # Recurse on the behaviors.
    @behaviors.each { |behavior| behavior.block.delete_related!(names) }
end

#delete_scope!(scope) ⇒ Object

Deletes a scope.



182
183
184
185
186
187
188
189
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 182

def delete_scope!(scope)
    # Remove the scope from the list
    @scopes.delete(scope)
    # And remove its parent.
    scope.parent = nil
    # Return the deleted scope
    scope
end

#delete_systemI!(systemI) ⇒ Object

Deletes a systemI.



203
204
205
206
207
208
209
210
211
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 203

def delete_systemI!(systemI)
    if @systemIs.key?(systemI.name) then
        # The instance is present, do remove it.
        @systemIs.delete(systemI.name)
        # And remove its parent.
        systemI.parent = nil
    end
    systemI
end

#delete_systemT!(systemT) ⇒ Object

Deletes a systemT.



171
172
173
174
175
176
177
178
179
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 171

def delete_systemT!(systemT)
    if @systemTs.key?(systemT.name) then
        # The systemT is present, delete it. 
        @systemTs.delete(systemT.name)
        # And remove its parent.
        systemT.parent = nil
    end
    systemT
end

#delete_type!(type) ⇒ Object

Deletes a type.



160
161
162
163
164
165
166
167
168
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 160

def delete_type!(type)
    if @types.key?(type.name) then
        # The type is present, delete it. 
        @types.delete(type.name)
        # And remove its parent.
        type.parent = nil
    end
    type
end

#delete_unless!(keep) ⇒ Object

Removes the signals and corresponding assignments whose name is not in +keep+.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/HDLRuby/hruby_low_cleanup.rb', line 80

def delete_unless!(keep)
    # Recurse on the sub scopes.
    self.each_scope { |scope| scope.delete_unless!(keep) }

    # Remove the unessary  inner signals.
    self.each_inner.to_a.each do |inner|
        unless keep.include?(inner.name) then
            self.delete_inner!(inner)
        end
    end

    # Remove the unessary connections.
    self.each_connection.to_a.each do |connection|
        # puts "connection with left=#{connection.left.name}"
        unless connection.left.each_node_deep.any? { |node|
            node.is_a?(RefName) && keep.include?(node.name) 
        }
        self.delete_connection!(connection)
        end
    end

    # Recurse on the blocks.
    self.each_behavior do |behavior|
        behavior.block.delete_unless!(keep)
    end
end

#each_behavior(&ruby_block) ⇒ Object

Iterates over the behaviors.

Returns an enumerator if no ruby block is given.



1055
1056
1057
1058
1059
1060
# File 'lib/HDLRuby/hruby_low.rb', line 1055

def each_behavior(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_behavior) unless ruby_block
    # A ruby block? Apply it on each behavior.
    @behaviors.each(&ruby_block)
end

#each_behavior_deep(&ruby_block) ⇒ Object

Iterates over all the behaviors of the system type and its system instances.



1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
# File 'lib/HDLRuby/hruby_low.rb', line 1095

def each_behavior_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_behavior_deep) unless ruby_block
    # A ruby block?
    # First recurse on the sub scopes.
    self.each_scope_deep do |scope|
        scope.each_behavior(&ruby_block)
    end
    # Then iterate over current system type's behavior.
    self.each_behavior(&ruby_block)
end

#each_block_deep(&ruby_block) ⇒ Object

Iterates over all the blocks of the system type and its system instances.



1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
# File 'lib/HDLRuby/hruby_low.rb', line 1124

def each_block_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_block_deep) unless ruby_block
    # A ruby block?
    # Then apply on each sub scope.
    self.each_scope do |scope|
        scope.each_block_deep(&ruby_block)
    end
    # And apply it on each behavior's block deeply.
    self.each_behavior do |behavior|
        behavior.each_block_deep(&ruby_block)
    end
end

#each_code(&ruby_block) ⇒ Object

Iterates over the non-HDLRuby code chunks.

Returns an enumerator if no ruby block is given.



831
832
833
834
835
836
837
# File 'lib/HDLRuby/hruby_low.rb', line 831

def each_code(&ruby_block)
    # puts "each_code from scope=#{self}"
    # No ruby block? Return an enumerator.
    return to_enum(:each_code) unless ruby_block
    # A ruby block? Apply it on each system instance.
    @codes.each(&ruby_block)
end

#each_connection(&ruby_block) ⇒ Object

Iterates over the connections.

Returns an enumerator if no ruby block is given.



1001
1002
1003
1004
1005
1006
# File 'lib/HDLRuby/hruby_low.rb', line 1001

def each_connection(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_connection) unless ruby_block
    # A ruby block? Apply it on each connection.
    @connections.each(&ruby_block)
end

#each_connection_deep(&ruby_block) ⇒ Object

Iterates over all the connections of the system type and its system instances.



1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
# File 'lib/HDLRuby/hruby_low.rb', line 1026

def each_connection_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_connection_deep) unless ruby_block
    # A ruby block?
    # First iterate over current system type's connection.
    self.each_connection(&ruby_block)
    # Then recurse on the system instances.
    self.each_systemI do |systemI|
        systemI.each_connection_deep(&ruby_block)
    end
end

#each_deep(&ruby_block) ⇒ Object

Iterates over each object deeply.

Returns an enumerator if no ruby block is given.



1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
# File 'lib/HDLRuby/hruby_low.rb', line 1234

def each_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_deep) unless ruby_block
    # A ruby block? First apply it to current.
    ruby_block.call(self)
    # The apply on each type.
    self.each_type do |type|
        type.each_deep(&ruby_block)
    end
    # Then apply on each systemT.
    self.each_systemT do |systemT|
        systemT.each_deep(&ruby_block)
    end
    # Then apply on each scope.
    self.each_scope do |scope|
        scope.each_deep(&ruby_block)
    end
    # Then apply on each inner signal.
    self.each_inner do |inner|
        inner.each_deep(&ruby_block)
    end
    # Then apply on each systemI.
    self.each_systemI do |systemI|
        systemI.each_deep(&ruby_block)
    end
    # Then apply on each code.
    self.each_code do |code|
        code.each_deep(&ruby_block)
    end
    # Then apply on each connection.
    self.each_connection do |connection|
        connection.each_deep(&ruby_block)
    end
    # Then apply on each behavior.
    self.each_behavior do |behavior|
        behavior.each_deep(&ruby_block)
    end
end

#each_inner(&ruby_block) ⇒ Object

Iterates over the inner signals.

Returns an enumerator if no ruby block is given.



871
872
873
874
875
876
877
# File 'lib/HDLRuby/hruby_low.rb', line 871

def each_inner(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_inner) unless ruby_block
    # A ruby block? Apply it on each inner signal instance.
    # @inners.each_value(&ruby_block)
    @inners.each(&ruby_block)
end

#each_node_deep(&ruby_block) ⇒ Object

Iterates over all the nodes of the system type and its system instances.



1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
# File 'lib/HDLRuby/hruby_low.rb', line 1169

def each_node_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_node_deep) unless ruby_block
    # A ruby block?
    # Then apply on each sub scope.
    self.each_scope do |scope|
        scope.each_node_deep(&ruby_block)
    end
    # And apply it on each behavior's block deeply.
    self.each_behavior do |behavior|
        behavior.each_node_deep(&ruby_block)
    end
end

#each_scope(&ruby_block) ⇒ Object

Iterates over the sub scopes.

Returns an enumerator if no ruby block is given.



722
723
724
725
726
727
# File 'lib/HDLRuby/hruby_low.rb', line 722

def each_scope(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_scope) unless ruby_block
    # A ruby block? Apply it on each sub scope.
    @scopes.each(&ruby_block)
end

#each_scope_deep(&ruby_block) ⇒ Object

Iterates over the scopes deeply.

Returns an enumerator if no ruby block is given.



732
733
734
735
736
737
738
739
# File 'lib/HDLRuby/hruby_low.rb', line 732

def each_scope_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_scope_deep) unless ruby_block
    # A ruby block? Apply it on self.
    ruby_block.call(self)
    # And recurse each sub scope.
    @scopes.each {|scope| scope.each_scope_deep(&ruby_block) }
end

#each_signal(&ruby_block) ⇒ Object

Iterates over all the signals (Equivalent to each_inner).

Returns an enumerator if no ruby block is given.



882
883
884
885
886
887
# File 'lib/HDLRuby/hruby_low.rb', line 882

def each_signal(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_signal) unless ruby_block
    # A ruby block? Apply it on each signal instance.
    @inners.each(&ruby_block)
end

#each_signal_deep(&ruby_block) ⇒ Object

Iterates over all the signals of the scope, its behaviors', its instances' and its sub scopes'.



891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
# File 'lib/HDLRuby/hruby_low.rb', line 891

def each_signal_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_signal_deep) unless ruby_block
    # A ruby block?
    # First iterate over the current system type's signals.
    self.each_signal(&ruby_block)
    # Then apply on the behaviors (since in HDLRuby:High, blocks can
    # include signals).
    self.each_behavior do |behavior|
        behavior.block.each_signal_deep(&ruby_block)
    end
    # Then recurse on the system instances.
    self.each_systemI do |systemI|
        systemI.each_signal_deep(&ruby_block)
    end
    # The recurse on the sub scopes.
    self.each_scope do |scope|
        scope.each_signal_deep(&ruby_block)
    end
end

#each_statement_deep(&ruby_block) ⇒ Object

Iterates over all the stamements of the system type and its system instances.



1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
# File 'lib/HDLRuby/hruby_low.rb', line 1153

def each_statement_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_statement_deep) unless ruby_block
    # A ruby block?
    # Then apply on each sub scope.
    self.each_scope do |scope|
        scope.each_statement_deep(&ruby_block)
    end
    # And apply it on each behavior's block deeply.
    self.each_behavior do |behavior|
        behavior.each_statement_deep(&ruby_block)
    end
end

#each_systemI(&ruby_block) ⇒ Object

Iterates over the system instances.

Returns an enumerator if no ruby block is given.



779
780
781
782
783
784
785
# File 'lib/HDLRuby/hruby_low.rb', line 779

def each_systemI(&ruby_block)
    # puts "each_systemI from scope=#{self}"
    # No ruby block? Return an enumerator.
    return to_enum(:each_systemI) unless ruby_block
    # A ruby block? Apply it on each system instance.
    @systemIs.each(&ruby_block)
end

#each_systemT(&ruby_block) ⇒ Object

Iterates over the system instances.

Returns an enumerator if no ruby block is given.



616
617
618
619
620
621
622
# File 'lib/HDLRuby/hruby_low.rb', line 616

def each_systemT(&ruby_block)
    # puts "each_systemT from scope=#{self}"
    # No ruby block? Return an enumerator.
    return to_enum(:each_systemT) unless ruby_block
    # A ruby block? Apply it on each system instance.
    @systemTs.each(&ruby_block)
end

#each_type(&ruby_block) ⇒ Object

Iterates over the system instances.

Returns an enumerator if no ruby block is given.



668
669
670
671
672
673
674
# File 'lib/HDLRuby/hruby_low.rb', line 668

def each_type(&ruby_block)
    # puts "each_type from scope=#{self}"
    # No ruby block? Return an enumerator.
    return to_enum(:each_type) unless ruby_block
    # A ruby block? Apply it on each system instance.
    @types.each(&ruby_block)
end

#eql?(obj) ⇒ Boolean

Comparison for hash: structural comparison.

Returns:

  • (Boolean)


541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/HDLRuby/hruby_low.rb', line 541

def eql?(obj)
    return false unless obj.is_a?(Scope)
    idx = 0
    obj.each_systemT do |systemT|
        return false unless @systemTs[systemT.name].eql?(systemT)
        idx += 1
    end
    return false unless idx == @systemTs.size
    idx = 0
    obj.each_type do |type|
        return false unless @types[type.name].eql?(type)
        idx += 1
    end
    return false unless idx == @types.size
    idx = 0
    obj.each_scope do |scope|
        return false unless @scopes[idx].eql?(scope)
        idx += 1
    end
    return false unless idx == @scopes.size
    idx = 0
    obj.each_inner do |inner|
        return false unless @inners[inner.name].eql?(inner)
        idx += 1
    end
    return false unless idx == @inners.size
    idx = 0
    obj.each_systemI do |systemI|
        return false unless @systemIs[systemI.name].eql?(systemI)
        idx += 1
    end
    return false unless idx == @systemIs.size
    idx = 0
    obj.each_connection do |connection|
        return false unless @connections[idx].eql?(connection)
        idx += 1
    end
    return false unless idx == @connections.size
    idx = 0
    obj.each_behavior do |behavior|
        return false unless @behaviors[idx].eql?(behavior)
        idx += 1
    end
    return false unless idx == @behaviors.size
    return true
end

#explicit_types!Object

Explicit the types conversions in the scope.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/HDLRuby/hruby_low_fix_types.rb', line 29

def explicit_types!
    # Recurse on the sub scopes.
    self.each_scope(&:explicit_types!)
    # Fix the types of the declarations.
    self.each_inner(&:explicit_types!)
    # Fix the types of the connections.
    self.each_connection(&:explicit_types!)
    # Fix the types of the behaviors.
    self.each_behavior(&:explicit_types!)
    return self
end

#extract_behaviors!Object

Extract the behaviors from the scope and returns them into an array.

NOTE: do not recurse into the sub scopes!



132
133
134
135
136
137
138
139
140
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 132

def extract_behaviors!
    # Get the behaviors.
    behs = self.each_behavior.to_a
    # Remove them from the scope.
    # behs.each { |beh| self.delete_behavior!(beh) }
    self.delete_all_behaviors!
    # Return the behaviors.
    return behs
end

#extract_connections!Object

Extract the connections from the scope and returns them into an array.

NOTE: do not recurse into the sub scopes!



145
146
147
148
149
150
151
152
153
154
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 145

def extract_connections!
    # Get the connections.
    cnxs = self.each_connection.to_a
    # Remove them from the scope.
    # cnxs.each { |cnx| self.delete_connection!(cnx) }
    # cnxs.delete_all_connections!
    self.delete_all_connections!
    # Return the connections.
    return cnxs
end

#extract_declares!Object

Extract the declares from the scope and returns them into an array.

NOTE: do not recurse into the sub scopes or behaviors!



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 159

def extract_declares!
    # Ensure there is a name.
    self.force_name!
    # The extracted declares.
    decls = []
    # Extract the types.
    types = []
    self.each_type {|type| types << type }
    types.each {|type| self.delete_type!(type) }
    # Renames them with the current level.
    types.each do |type|
        former = type.name
        self.extend_name!(type)
        self.replace_names_subs!(former,type.name)
    end
    # Adds the types
    decls << types
    # Extract the systemTs.
    systemTs = []
    self.each_systemT {|systemT| systemTs << systemT }
    systemTs.each {|systemT| self.delete_systemT!(systemT) }
    # Renames them with the current level.
    systemTs.each do |systemT|
        former = systemT.name
        self.extend_name!(systemT)
        self.replace_names_subs!(former,systemT.name)
    end
    # Adds the systemTs
    decls << systemTs
    # Extract the inners.
    inners = []
    self.each_inner {|inner| inners << inner }
    inners.each {|inner| self.delete_inner!(inner) }
    # Renames them with the current level.
    inners.each do |inner|
        former = inner.name
        self.extend_name!(inner)
        self.replace_names_subs!(former,inner.name)
    end
    # Adds the inners
    decls << inners
    # Extract the systemIs
    systemIs = []
    self.each_systemI {|systemI| systemIs << systemI }
    systemIs.each {|systemI| self.delete_systemI!(systemI) }
    # Renames them with the current level.
    systemIs.each do |systemI|
        former = systemI.name
        self.extend_name!(systemI)
        self.replace_names_subs!(former,systemI.name)
    end
    # Adds the systemIs
    decls << systemIs
    # Returns the extracted declares.
    return decls
end

#extract_port_assign!(systemI, signal) ⇒ Object

Extracts the assignments to port +systemI.signal+ and returns the resulting reference to a port wire.

NOTE: assumes to_upper_space! and with_port! has been called.



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/HDLRuby/hruby_low2vhd.rb', line 419

def extract_port_assign!(systemI,signal)
    # Extract the assignment.
    assign = nil
    self.each_connection.to_a.each do |connection|
        if self.port_assign?(connection.left,systemI,signal) then
            # The left is the port.
            # Delete the connection.
            self.delete_connection!(connection)
            # And return a copy of the right.
            return connection.right.clone
        elsif self.port_assign?(connection.right,systemI,signal) then
            # The right is the port.
            # Delete the connection.
            self.delete_connection!(connection)
            # And return a copy of the left.
            return connection.left.clone
        end
    end
    # No port found, nothing to do
    return nil
end

#get_all_innersObject

Gets an array containing all the inner signals.



923
924
925
# File 'lib/HDLRuby/hruby_low.rb', line 923

def get_all_inners
    return each_inner.to_a
end

#get_by_name(name) ⇒ Object

Find an inner object by +name+. NOTE: return nil if not found.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/HDLRuby/hruby_low_resolve.rb', line 41

def get_by_name(name)
    # puts "getbyname for name=#{name} with self=#{self}"
    # Ensure the name is a symbol.
    name = name.to_sym
    # Look in the signals.
    found = self.get_inner(name)
    return found if found
    # Look in the instances.
    found = self.each_systemI.find { |systemI| systemI.name == name }
    return found if found
    # Maybe it is a sub scope.
    return self.each_scope.find { |scope| scope.name == name }
    # Maybe it in the behavior.
    return self.behavior.get_by_name
end

#get_code(name) ⇒ Object

Gets a code chunk by +name+.



845
846
847
# File 'lib/HDLRuby/hruby_low.rb', line 845

def get_code(name)
    return @codes[name]
end

#get_inner(name) ⇒ Object

Gets an inner signal by +name+.



928
929
930
# File 'lib/HDLRuby/hruby_low.rb', line 928

def get_inner(name)
    return @inners[name.to_sym]
end

#get_signal(name) ⇒ Object

Gets an inner signal by +name+, equivalent to get_inner.



968
969
970
# File 'lib/HDLRuby/hruby_low.rb', line 968

def get_signal(name)
    return @inners[name]
end

#get_systemI(name) ⇒ Object

Gets a system instance by +name+.



793
794
795
# File 'lib/HDLRuby/hruby_low.rb', line 793

def get_systemI(name)
    return @systemIs[name]
end

#get_systemT(name) ⇒ Object

Gets a system instance by +name+.



630
631
632
# File 'lib/HDLRuby/hruby_low.rb', line 630

def get_systemT(name)
    return @systemTs[name]
end

#get_type(name) ⇒ Object

Gets a system instance by +name+.



682
683
684
# File 'lib/HDLRuby/hruby_low.rb', line 682

def get_type(name)
    return @types[name]
end

#has_behavior?Boolean

Tells if there is any inner.

Returns:

  • (Boolean)


1108
1109
1110
# File 'lib/HDLRuby/hruby_low.rb', line 1108

def has_behavior?
    return !@behaviors.empty?
end

#has_code?Boolean

Tells if there is any non-HDLRuby code chunk.

Returns:

  • (Boolean)


840
841
842
# File 'lib/HDLRuby/hruby_low.rb', line 840

def has_code?
    return !@codes.empty?
end

#has_connection?Boolean

Tells if there is any connection.

Returns:

  • (Boolean)


1009
1010
1011
# File 'lib/HDLRuby/hruby_low.rb', line 1009

def has_connection?
    return !@connections.empty?
end

#has_inner?Boolean

Tells if there is any inner.

Returns:

  • (Boolean)


913
914
915
# File 'lib/HDLRuby/hruby_low.rb', line 913

def has_inner?
    return !@inners.empty?
end

#has_scope?Boolean

Tells if there is any sub scope.

Returns:

  • (Boolean)


742
743
744
# File 'lib/HDLRuby/hruby_low.rb', line 742

def has_scope?
    return !@scopes.empty?
end

#has_signal?Boolean

Tells if there is any signal, equivalent to has_inner?

Returns:

  • (Boolean)


918
919
920
# File 'lib/HDLRuby/hruby_low.rb', line 918

def has_signal?
    return self.has_inner?
end

#has_systemI?Boolean

Tells if there is any system instance.

Returns:

  • (Boolean)


788
789
790
# File 'lib/HDLRuby/hruby_low.rb', line 788

def has_systemI?
    return !@systemIs.empty?
end

#has_systemT?Boolean

Tells if there is any system instance.

Returns:

  • (Boolean)


625
626
627
# File 'lib/HDLRuby/hruby_low.rb', line 625

def has_systemT?
    return !@systemTs.empty?
end

#has_type?Boolean

Tells if there is any system instance.

Returns:

  • (Boolean)


677
678
679
# File 'lib/HDLRuby/hruby_low.rb', line 677

def has_type?
    return !@types.empty?
end

#hashObject

Hash function.



589
590
591
# File 'lib/HDLRuby/hruby_low.rb', line 589

def hash
    return [@systemTs,@types,@scopes,@inners,@systemIs,@connections,@behaviors].hash
end

#initial_concat_to_timed!Object

Converts initial array of value of signals to assignment in timed blocks (for making the code compatible with verilog translation).

NOTE: Assumes such array as at the top level.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/HDLRuby/hruby_low_without_concat.rb', line 65

def initial_concat_to_timed!
    # Gather the signal with concat as initial values.
    sigs = []
    # For the interface signals of the upper system.
    self.parent.each_signal do |sig|
        sigs << sig if sig.value.is_a?(Concat)
    end
    # For the inner signals of the scope.
    self.each_signal do |sig|
        sigs << sig if sig.value.is_a?(Concat)
    end
    # No initial concat? End here.
    return if sigs.empty?
    
    # Create a timed block for moving the concat initialization
    # to it.
    initial = TimeBlock.new(:seq)
    self.add_behavior(TimeBehavior.new(initial))
    # Adds to it the initializations.
    sigs.each do |sig|
        name = sig.name
        styp = sig.type
        btyp = styp.base
        value = sig.value
        sig.value.each_expression.with_index do |expr,i|
            left = RefIndex.new(btyp,
                                RefName.new(styp,RefThis.new,name),
                                i.to_expr)
            initial.add_statement(Transmit.new(left,expr.clone))
        end
    end
    # Remove the initial values from the signals.
    sigs.each do |sig|
        sig.set_value!(nil)
    end
end

#instance_port?(node) ⇒ Boolean

Tells if a +node+ is a reference to an instance's port.

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/HDLRuby/hruby_low_with_port.rb', line 65

def instance_port?(node)
    # First the node must be a name reference.
    return false unless node.is_a?(RefName)
    # Then its sub ref must be a RefName of an instance.
    sub = node.ref
    return false unless sub.is_a?(RefName)
    # puts "@systemIs.keys=#{@systemIs.keys}"
    # System instance in current scope?
    return true if @systemIs.key?(sub.name)
    # if self.parent.is_a?(Scope) then
    #     # Recurse the search in the parent.
    #     return parent.instance_port?(node)
    # else
    #     # No parent, failure.
    #     return false
    # end
    return false
end

#last_behaviorObject

Returns the last behavior.



1073
1074
1075
# File 'lib/HDLRuby/hruby_low.rb', line 1073

def last_behavior
    return @behaviors[-1]
end

#make_portw(ref) ⇒ Object

Generates a port wire from a reference.



57
58
59
60
61
62
# File 'lib/HDLRuby/hruby_low_with_port.rb', line 57

def make_portw(ref)
    # First generates the name of the port.
    name = sym2portw_name(ref.to_sym)
    # Then generate the port wire.
    return SignalI.new(name,ref.type)
end

#map_behaviors!(&ruby_block) ⇒ Object

Maps on the behaviors.



151
152
153
154
155
156
157
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 151

def map_behaviors!(&ruby_block)
    @behaviors.map! do |behavior|
        behavior = ruby_block.call(behavior)
        behavior.parent = self unless behavior.parent
        behavior
    end
end

#map_connections!(&ruby_block) ⇒ Object

Maps on the connections.



142
143
144
145
146
147
148
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 142

def map_connections!(&ruby_block)
    @connections.map! do |connection|
        connection = ruby_block.call(connection)
        connection.parent = self unless connection.parent
        connection
    end
end

#map_inners!(&ruby_block) ⇒ Object

Maps on the inners.



124
125
126
127
128
129
130
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 124

def map_inners!(&ruby_block)
    @inners.map! do |inner|
        inner = ruby_block.call(inner)
        inner.parent = self unless inner.parent
        inner
    end
end

#map_scopes!(&ruby_block) ⇒ Object

Maps on the scopes.



115
116
117
118
119
120
121
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 115

def map_scopes!(&ruby_block)
    @scopes.map! do |scope|
        scope = ruby_block.call(scope)
        scope.parent = self unless scope.parent
        scope
    end
end

#map_systemIs!(&ruby_block) ⇒ Object

Maps on the systemIs.



133
134
135
136
137
138
139
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 133

def map_systemIs!(&ruby_block)
    @systemIs.map! do |systemI|
        systemI = ruby_block.call(systemI)
        systemI.parent = self unless systemI.parent
        systemI
    end
end

#map_systemTs!(&ruby_block) ⇒ Object

Maps on the local systemTs.



110
111
112
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 110

def map_systemTs!(&ruby_block)
    @systemTs.map(&ruby_block)
end

#map_types!(&ruby_block) ⇒ Object

Maps on the local types.



105
106
107
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 105

def map_types!(&ruby_block)
    @types.map(&ruby_block)
end

#mixblocks2seq!Object

Converts the par sub blocks to seq if they are not full par.



48
49
50
51
# File 'lib/HDLRuby/hruby_low2seq.rb', line 48

def mixblocks2seq!
    # Recurse on the behaviors.
    self.each_behavior { |beh| beh.mixblocs2seq! }
end

#par_in_seq2seq!Object

Converts par blocks within seq blocks to seq blocks.



30
31
32
33
34
35
36
37
# File 'lib/HDLRuby/hruby_low_without_parinseq.rb', line 30

def par_in_seq2seq!
    # Recruse on the sub scopes.
    self.each_scope(&:par_in_seq2seq!)
    # Recurse on the block.
    self.each_behavior do |behavior|
        behavior.block.par_in_seq2seq!
    end
end

#parent_systemObject

Gets the parent system, i.e., the parent of the top scope.



1281
1282
1283
# File 'lib/HDLRuby/hruby_low.rb', line 1281

def parent_system
    return self.top_scope.parent
end

#port_assign?(expr, systemI, signal) ⇒ Boolean

Tells if an expression is a reference to port +systemI.signal+.

Returns:

  • (Boolean)


410
411
412
413
# File 'lib/HDLRuby/hruby_low2vhd.rb', line 410

def port_assign?(expr, systemI, signal)
    return expr.is_a?(RefName) && expr.name == signal.name &&
        expr.ref.is_a?(RefName) && expr.ref.name == systemI.name
end

#portw2ref(portw) ⇒ Object

Converts a port wire to a reference to it.



40
41
42
# File 'lib/HDLRuby/hruby_low_with_port.rb', line 40

def portw2ref(portw)
    return RefName.new(portw.type,RefThis.new,portw.name)
end

#portw_name2sym(name) ⇒ Object

Converts a port wire +name+ to the symbol giving the corresponding HDLRuby reference.



52
53
54
# File 'lib/HDLRuby/hruby_low_with_port.rb', line 52

def portw_name2sym(name)
    return name[1..-1].to_sym
end

#replace_names!(former, nname) ⇒ Object

Replaces recursively +former+ name by +nname+ until it is redeclared.



244
245
246
247
248
249
250
251
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 244

def replace_names!(former,nname)
    # Stop here if the name is redeclared.
    return if self.each_type.find {|type| type.name == former }
    return if self.each_systemT.find {|systemT| systemT.name == former }
    return if self.each_inner.find {|inner| inner.name == former }
    # Recurse on the internals.
    replace_names_subs!(former,nname)
end

#replace_names_subs!(former, nname) ⇒ Object

Replaces recursively +former+ name by +nname+ until it is redeclared in the internals.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 218

def replace_names_subs!(former,nname)
    # puts "replace_names_subs! for #{self} with former=#{former} and nname=#{nname}"
    self.each_type do |type|
        type.replace_names!(former,nname)
    end
    self.each_systemT do |systemT|
        systemT.replace_names!(former,nname)
    end
    self.each_scope do |scope|
        scope.replace_names!(former,nname)
    end
    self.each_inner do |inner|
        inner.replace_names!(former,nname)
    end
    self.each_systemI do |systemI|
        systemI.replace_names!(former,nname)
    end
    self.each_connection do |connection|
        connection.replace_names!(former,nname)
    end
    self.each_behavior do |behavior|
        behavior.replace_names!(former,nname)
    end
end

#reverse_each_behavior(&ruby_block) ⇒ Object

Reverse iterates over the behaviors.

Returns an enumerator if no ruby block is given.



1065
1066
1067
1068
1069
1070
# File 'lib/HDLRuby/hruby_low.rb', line 1065

def reverse_each_behavior(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:reverse_each_behavior) unless ruby_block
    # A ruby block? Apply it on each behavior.
    @behaviors.reverse_each(&ruby_block)
end

#select2case!Object

Converts the Select expressions to Case statements.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/HDLRuby/hruby_low_without_select.rb', line 71

def select2case!
    # Recruse on the sub scopes.
    self.each_scope(&:select2case!)

    # Recurse on the blocks.
    self.each_behavior do |behavior|
        behavior.block.each_block_deep(&:select2case!)
    end

    # Work on the connections.
    self.each_connection.to_a.each do |connection|
        selects = connection.extract_selects!
        if selects.any? then
            # Selects have been extract, replace the connection
            # be y behavior.
            # Generate the block with cases.
            blk = LowWithoutSelect.selects2block(selects)
            # Add a transmit replacing the connection.
            blk.add_statement(
                Transmit.new(connection.left.clone,
                             connection.right.clone))
            # Remove the connection and add a behavior instead.
            self.delete_connection!(connection)
            self.add_behavior(Behavior.new(blk))
        end
    end
end

#sym2portw_name(sym) ⇒ Object

Converts symbol +sym+ representing an HDLRuby reference to a instance port to a port wire.



46
47
48
# File 'lib/HDLRuby/hruby_low_with_port.rb', line 46

def sym2portw_name(sym)
    return ("^" + sym.to_s).to_sym
end

#to_c(res, level = 0) ⇒ Object

Generates the C text of the equivalent HDLRuby code. +level+ is the hierachical level of the object. def to_c(level = 0)



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/HDLRuby/hruby_low2c.rb', line 410

def to_c(res,level = 0)
    # The resulting string.
    # res = ""

    # Declare the global variable holding the scope.
    res << "Scope " << Low2C.obj_name(self) << ";\n\n"

    # Generate the code makeing the complex sub components.

    # Generates the code for making signals if any.
    # self.each_signal { |signal| res << signal.to_c(level) }
    self.each_signal { |signal| signal.to_c(res,level) }
    # Generates the code for making signals if any.
    # self.each_systemI { |systemI| res << systemI.to_c(level) }
    self.each_systemI { |systemI| systemI.to_c(res,level) }
    # Generates the code for making sub scopes if any.
    # self.each_scope { |scope| res << scope.to_c(level) }
    self.each_scope { |scope| scope.to_c(res,level) }
    # Generate the code for making the behaviors.
    # self.each_behavior { |behavior| res << behavior.to_c(level) }
    self.each_behavior { |behavior| behavior.to_c(res,level) }
    # Generate the code for making the non-HDLRuby codes.
    # self.each_code { |code| res << code.to_c(level) }
    self.each_code { |code| code.to_c(res,level) }

    # Generate the code of the scope.
    
    # The header of the scope.
    res << " " * level*3
    res << "Scope " << Low2C.make_name(self) << "() {\n"
    res << " " * (level+1)*3
    res << "Scope scope = malloc(sizeof(ScopeS));\n"
    res << " " * (level+1)*3
    res << "scope->kind = SCOPE;\n";

    # Sets the global variable of the scope.
    res << "\n"
    res << " " * (level+1)*3
    res << Low2C.obj_name(self) << " = scope;\n"

    # Set the owner if any.
    if self.parent then
        res << " " * (level+1)*3
        res << "scope->owner = (Object)"
        res << Low2C.obj_name(self.parent) << ";\n"
    else
        res << "scope->owner = NULL;\n"
    end

    # The name
    res << " " * (level+1)*3
    res << "scope->name = \"#{self.name}\";\n"

    # Add the system instances declaration.
    res << " " * (level+1)*3
    res << "scope->num_systemIs = #{self.each_systemI.to_a.size};\n"
    res << " " * (level+1)*3
    res << "scope->systemIs = calloc(sizeof(SystemI)," +
           "scope->num_systemIs);\n"
    self.each_systemI.with_index do |systemI,i|
        res << " " * (level+1)*3
        res << "scope->systemIs[#{i}] = "
        res << Low2C.make_name(systemI) << "();\n"
    end

    # Add the inner signals declaration.
    res << " " * (level+1)*3
    res << "scope->num_inners = #{self.each_inner.to_a.size};\n"
    res << " " * (level+1)*3
    res << "scope->inners = calloc(sizeof(SignalI),"
    res << "scope->num_inners);\n"
    self.each_inner.with_index do |inner,i|
        res << " " * (level+1)*3
        res << "scope->inners[#{i}] = "
        res << Low2C.make_name(inner) << "();\n"
    end

    # Add the sub scopes.
    res << " " * (level+1)*3
    res << "scope->num_scopes = #{self.each_scope.to_a.size};\n"
    res << " " * (level+1)*3
    res << "scope->scopes = calloc(sizeof(Scope),"
    res << "scope->num_scopes);\n"
    self.each_scope.with_index do |scope,i|
        res << " " * (level+1)*3
        res << "scope->scopes[#{i}] = "
        res << Low2C.make_name(scope) << "();\n"
    end

    # Add the behaviors.
    res << " " * (level+1)*3
    res << "scope->num_behaviors = #{self.each_behavior.to_a.size};\n"
    res << " " * (level+1)*3
    res << "scope->behaviors = calloc(sizeof(Behavior),"
    res << "scope->num_behaviors);\n"
    self.each_behavior.with_index do |behavior,i|
        res << " " * (level+1)*3
        res << "scope->behaviors[#{i}] = "
        res << Low2C.make_name(behavior) << "();\n"
    end

    # Add the non-HDLRuby codes.
    res << " " * (level+1)*3
    res << "scope->num_codes = #{self.each_code.to_a.size};\n"
    res << " " * (level+1)*3
    res << "scope->codes = calloc(sizeof(Code)," +
           "scope->num_codes);\n"
    self.each_code.with_index do |code,i|
        res << " " * (level+1)*3
        res << "scope->codes[#{i}] = "
        res << Low2C.make_name(code) << "();\n"
    end

    # Generate the Returns of the result.
    res << "\n"
    res << " " * (level+1)*3
    res << "return scope;\n"

    # Close the scope.
    res << " " * level*3
    res << "}\n\n"
    return res
end

#to_ch(res) ⇒ Object

Generates the content of the h file. def to_ch



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/HDLRuby/hruby_low2c.rb', line 536

def to_ch(res)
    # res = ""
    # Declare the global variable holding the signal.
    res << "extern Scope " << Low2C.obj_name(self) << ";\n\n"

    # Generate the access to the function making the scope.
    res << "extern Scope " << Low2C.make_name(self) << "();\n\n"

    # Generate the accesses to the system instances.
    # self.each_systemI { |systemI| res << systemI.to_ch }
    self.each_systemI { |systemI| systemI.to_ch(res) }

    # Generate the accesses to the signals.
    # self.each_inner { |inner| res << inner.to_ch }
    self.each_inner { |inner| inner.to_ch(res) }

    # Generate the access to the sub scopes.
    # self.each_scope { |scope| res << scope.to_ch }
    self.each_scope { |scope| scope.to_ch(res) }

    # Generate the access to the behaviors.
    # self.each_behavior { |behavior| res << behavior.to_ch }
    self.each_behavior { |behavior| behavior.to_ch(res) }

    # Generate the access to the non-HDLRuby code.
    # self.each_behavior { |code| res << code.to_ch }
    self.each_behavior { |code| code.to_ch(res) }

    return res;
end

#to_hdr(level = 0, header = true) ⇒ Object

Generates the text of the equivalent hdr text. +level+ is the hierachical level of the object and +header+ tells if the header is to generate or not.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/HDLRuby/hruby_low2hdr.rb', line 111

def to_hdr(level = 0,header = true)
    # The resulting string.
    res = ""
    # Generate the header if required.
    if header then
        res << (" " * (level*3)) << "sub "
        unless self.name.empty? then
            res << ":" << Low2HDR.hdr_decl_name(self.name) << " "
        end
        res << "do\n"
    end
    level = level + 1 if header
    # Generate the sub types.
    # Assume the types are TypeDef.
    self.each_type do |type|
        res << " " * (level*3)
        res << "typedef :#{type.name} do\n"
        res << " " * ((level+1)*3) << type.def.to_hdr(level)
        res << " " * (level*3) << "end\n"
    end
    # Generaste the sub system types.
    self.each_systemT { |systemT| res << systemT.to_hdr(level) }
    # Generate the inners declaration.
    self.each_inner do |inner|
        res << " " * (level*3)
        res << inner.type.to_high(level) 
        res << ".inner :" << Low2HDR.hdr_decl_name(inner.name) << "\n"
    end
    # Generate the instances.
    res << "\n" if self.each_inner.any?
    self.each_systemI do |systemI| 
        res << " " * (level*3)
        res << systemI.to_hdr(level) << "\n"
    end
    # Generate the sub scopes.
    self.each_scope do |scope|
        res << scope.to_hdr(level)
    end
    # Generate the connections.
    res << "\n" if self.each_scope.any?
    self.each_connection do |connection|
        res << connection.to_hdr(level)
    end
    # Generate the behaviors.
    res << "\n" if self.each_connection.any?
    self.each_behavior do |behavior|
        res << behavior.to_hdr(level)
    end
    # Close the scope if required.
    if header then
        res << " " * ((level-1)*3) << "end\n"
    end
    # Return the result.
    return res
end

#to_highObject

Creates a new high scope.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/HDLRuby/hruby_low2high.rb', line 34

def to_high
    # Create the high scope.
    res = new HDLRuby::High::Scope.new(self.name)
    # Add the local types.
    self.each_type { |t| res.add_type(t.to_high) }
    # Add the local system types.
    self.each_systemT { |s| res.add_systemT(s.to_high) }
    # Add the sub scopes.
    self.each_scope { |s| res.add_scope(s.to_high) }
    # Add the inner signals.
    self.each_inner { |i| res.add_inner(i.to_high) }
    # Add the system instances.
    self.each_systemI { |s| res.add_systemI(s.to_high) }
    # Add the non-HDLRuby cofe chunks.
    self.each_code { |c| res.add_code(c.to_high) }
    # Add the connections.
    self.each_connection { |c| res.add_connection(c.to_high) }
    # Add the behaviors.
    self.each_behavior { |b| res.add_behavior(b.to_high) }
    return res
end

#to_upper_space!Object

Moves the declarations to the upper namespace.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 88

def to_upper_space!
    # First recurse.
    # On the sub scopes.
    self.each_scope(&:to_upper_space!)
    # On the behaviors.
    self.each_behavior(&:to_upper_space!)
    
    # Then extract the declarations from the sub scope.
    decls = self.each_scope.map(&:extract_declares!)
    # And do the same with the behaviors'.
    decls << self.each_behavior.map(&:extract_declares!)

    # Reinsert the extracted declares to self.
    decls.flatten.each do |decl|
        if decl.is_a?(Type) then
            self.add_type(decl)
        elsif decl.is_a?(SystemT) then
            self.add_systemT(decl)
        elsif decl.is_a?(SignalI) then
            self.add_inner(decl)
        elsif decl.is_a?(SystemI) then
            self.add_systemI(decl)
        else
            raise AnyError, "Internal error: invalid class for a declaration: #{decl.class}"
        end
    end

    # Extract the behaviors of the sub scopes.
    behs = self.each_scope.map(&:extract_behaviors!).flatten
    # Reinsert them to self.
    behs.each { |beh| self.add_behavior(beh) }

    # Extract the connections of the sub scopes.
    cnxs = self.each_scope.map(&:extract_connections!).flatten
    # Reinsert them to self.
    cnxs.each { |beh| self.add_connection(beh) }

    # Now can delete the sub scopes since they are empty.
    self.each_scope.to_a.each { |scope| self.delete_scope!(scope) }
end

#to_vhdl(level = 0) ⇒ Object

Generates the text of the equivalent HDLRuby::High code. +level+ is the hierachical level of the object and



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/HDLRuby/hruby_low2vhd.rb', line 443

def to_vhdl(level = 0)
    # The resulting string.
    res = ""

    # Generate the architecture's header
    # The instances' headers
    self.each_systemI do |systemI|
        systemT = systemI.systemT
        # Its entity
        res << (" " * level*3)
        res << "component #{Low2VHDL.entity_name(systemT.name)}\n"
        res << (" " * (level+1)*3)
        # Its ports
        res << "port(\n"
        # Inputs
        systemT.each_input do |input|
            res << " " * ((level+2)*3)
            res << Low2VHDL.vhdl_name(input.name) << ": in " 
            res << input.type.to_vhdl << ";\n"
        end
        # Outputs
        systemT.each_output do |output|
            res << " " * ((level+2)*3)
            res << Low2VHDL.vhdl_name(output.name) << ": out " 
            res << output.type.to_vhdl << ";\n"
        end
        # Inouts
        systemT.each_inout do |inout|
            res << " " * ((level+2)*3)
            res << Low2VHDL.vhdl_name(inout.name) << ": inout " 
            res << inout.type.to_vhdl << ";\n"
        end
        # Remove the last ";" for conforming with VHDL syntax.
        res[-2..-1] = "\n" if res[-2] == ";"
        res << " " * ((level+1)*3)
        # Close the port declaration.
        res << ");\n"
        # Close the component.
        res << " " * (level*3)
        res << "end component;\n\n" 
    end

    # Generate the architecture's type definition.
    # It is assumed that these types are all TypeDef.
    self.each_type do |type|
        res << (" " * level*3)
        res << "type #{Low2VHDL.vhdl_name(type.name)} is "
        res << type.def.to_vhdl(level+1)
        res << ";\n"
    end

    ## Generates the required mux functions.
    mtps = [] # The mux functions to generate by type.
    # Gather the mux functions to generate.
    self.each_scope_deep do |scope|
        # Checks the connections.
        scope.each_connection do |connection|
            connection.right.each_node_deep do |node|
                if node.is_a?(Select) then
                    mtps << [node.type,node.each_choice.to_a.size]
                end
            end
        end
        # Checks the statements.
        scope.each_behavior do |behavior|
            behavior.block.each_node_deep do |node|
                if node.is_a?(Select) then
                    mtps << [node.type,node.each_choice.to_a.size]
                end
            end
        end
    end
    # Generate the gathered functions (only one per type).
    mtps.uniq!
    mtps.each do |type,num|
        res << Low2VHDL.mux_function(type,num," " * level*3)
    end

    # Generate the inner signals declaration.
    self.each_inner do |inner|
        res << " " * (level * 3)
        # General signal or constant signal?
        res << (inner.is_a?(SignalC) ?  "constant " : "signal ")
        # Signal name.
        res << Low2VHDL.vhdl_name(inner.name) << ": "
        # Signal type.
        res << inner.type.to_vhdl(level) 
        # Signal value.
        if inner.value then
            if inner.value.is_a?(Concat) then
                # Concat are to be given the expected type of the
                # elements for casting them equally.
                # res << " := " << inner.value.to_vhdl(inner.type.base,level)
                res << " := " << inner.value.to_vhdl(level,inner.type.base)
            else
                res << " := " << inner.value.to_vhdl(level)
            end
        end
        res << ";\n"
    end

    # Generate the architecture's content.
    res << " " * ((level-1)*3) << "begin\n"

    # Generate the instances connections.
    self.each_systemI do |systemI| 
        # Its Declaration.
        res << " " * (level*3)
        res << Low2VHDL.vhdl_name(systemI.name) << ": "
        systemT = systemI.systemT
        res << Low2VHDL.entity_name(systemT.name).to_s << "\n"
        res << " " * ((level+1)*3)
        # Its ports
        res << "port map(\n"
        # Inputs
        systemT.each_input do |input|
            ref = self.extract_port_assign!(systemI,input)
            if ref then
                res << " " * ((level+2)*3)
                res << Low2VHDL.vhdl_name(input.name) << " => " 
                res << ref.to_vhdl(level) 
                res << ",\n"
            end
        end
        # Outputs
        systemT.each_output do |output|
            ref = self.extract_port_assign!(systemI,output)
            if ref then
                res << " " * ((level+2)*3)
                res << Low2VHDL.vhdl_name(output.name) << " => " 
                res << ref.to_vhdl(level) 
                res << ",\n"
            end
        end
        # Inouts
        systemT.each_inout do |inout|
            ref = self.extract_port_assign!(systemI,inout)
            if ref then
                res << " " * ((level+2)*3)
                res << Low2VHDL.vhdl_name(inout.name) << " => " 
                res << ref.to_vhdl(level) 
                res << ",\n"
            end
        end
        # Remove the last ";" for conforming with VHDL syntax.
        res[-2..-1] = "\n" if res[-2] == ","
        # Close the port map declaration.
        res << " " * ((level+1)*3)
        res << ");\n"
    end
    # Generate the connections.
    res << "\n" if self.each_scope.any?
    self.each_scope_deep do |scope|
        scope.each_connection do |connection|
            res << connection.to_vhdl([],level)
        end
    end

    # Generate the behaviors.
    # Current scope's
    res << "\n" if self.each_connection.any?
    self.each_scope_deep do |scope|
        scope.each_behavior do |behavior|
            res << behavior.to_vhdl(level)
        end
    end
    return res
end

#top_scopeObject

Gets the top scope, i.e. the first scope of the current system.



1276
1277
1278
# File 'lib/HDLRuby/hruby_low.rb', line 1276

def top_scope
    return self.parent.is_a?(SystemT) ? self : self.parent.top_scope
end

#with_port!Object

Converts to a port-compatible system.

NOTE: the result is the same scope.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/HDLRuby/hruby_low_with_port.rb', line 88

def with_port!
    # # Recurse on the sub scope.
    # self.each_scope(&:with_port!)
    # Gather the references to instance ports.
    # Also remember if the references were left values or not.
    # And remember where the node was.
    refs = []
    ref_sym2leftvalue = {}
    # ref_parents = Set.new
    ref_parents = []
    self.each_block_deep do |block|
        block.each_node_deep do |node|
            if instance_port?(node) then
                # puts "port for node: #{node.ref.name}.#{node.name}"
                refs << node 
                ref_sym2leftvalue[node.to_sym] = node.leftvalue?
                ref_parents << node.parent
                # ref_parents[node.parent] = node
            end
        end
    end
    self.each_connection do |connection|
        connection.each_node_deep do |node|
            if instance_port?(node) then
                # puts "port for node: #{node.ref.name}.#{node.name}"
                # puts "leftvalue? #{node.leftvalue?}"
                refs << node 
                ref_sym2leftvalue[node.to_sym] = node.leftvalue?
                ref_parents << node.parent
                # ref_parents[node.parent] = node
            end
        end
    end
    # Generate the port wire from the refs.
    ref_sym2portw = {}
    refs.each { |ref| ref_sym2portw[ref.to_sym] = make_portw(ref) }
    # Declare the port wires.
    ref_sym2portw.each_value { |portw| self.add_inner(portw.clone) }
    # Replace the references by their corresponding port wires.
    self.each_block_deep do |block|
        block.each_node_deep do |node|
            if ref_parents.include?(node) then
                node.map_nodes! do |expr|
                    next expr unless instance_port?(expr)
                    portw = ref_sym2portw[expr.to_sym]
                    portw ? portw2ref(portw) : expr
                end
            end
        end
    end
    self.each_connection do |connection|
        connection.each_node_deep do |node|
            if ref_parents.include?(node) then
                node.map_nodes! do |expr|
                    next expr unless instance_port?(expr)
                    portw = ref_sym2portw[expr.to_sym]
                    portw ? portw2ref(portw) : expr
                end
            end
        end
    end

    # Finally adds the connections with the port wires.
    ref_sym2portw.each do |sym,portw|
        ref = sym.to_hdr
        if ref_sym2leftvalue[sym] then
            # The reference was a left value, assign the port wire
            # to the ref.
            self.add_connection(
                Connection.new(ref.clone,portw2ref(portw)) )
        else
            # The reference was a right value, assign it to the
            # port wire.
            self.add_connection(
                Connection.new(portw2ref(portw),ref.clone) )
        end
    end
    

    return self
end