Class: HDLRuby::Low::When

Inherits:
Object
  • Object
show all
Includes:
Hparent, Low2Symbol
Defined in:
lib/HDLRuby/hruby_low.rb,
lib/HDLRuby/hruby_low2c.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_skeleton.rb,
lib/HDLRuby/hruby_low_with_var.rb,
lib/HDLRuby/hruby_low_fix_types.rb,
lib/HDLRuby/hruby_low_bool2select.rb,
lib/HDLRuby/hruby_low_without_select.rb,
lib/HDLRuby/hruby_low_without_namespace.rb

Overview

Extends the When class with functionality for moving the declarations to the upper namespace.

Direct Known Subclasses

High::When

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 Low2Symbol

#to_sym

Methods included from Hparent

#scope

Constructor Details

#initialize(match, statement) ⇒ When

Creates a new when for a casde statement that executes +statement+ on +match+.



2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
# File 'lib/HDLRuby/hruby_low.rb', line 2900

def initialize(match,statement)
    # Checks the match.
    unless match.is_a?(Expression)
        raise AnyError, "Invalid class for a case match: #{match.class}"
    end
    # Checks statement.
    unless statement.is_a?(Statement)
        raise AnyError,
              "Invalid class for a statement: #{statement.class}"
    end
    # Set the match.
    @match = match
    # Set the statement.
    @statement = statement
    # And set their parents.
    match.parent = statement.parent = self
end

Instance Attribute Details

#matchObject (readonly)

The value to match.



2894
2895
2896
# File 'lib/HDLRuby/hruby_low.rb', line 2894

def match
  @match
end

#statementObject (readonly)

The statement to execute in in case of match.



2896
2897
2898
# File 'lib/HDLRuby/hruby_low.rb', line 2896

def statement
  @statement
end

Instance Method Details

#add_blocks_code(res, level) ⇒ Object

Adds the c code of the blocks to +res+ at +level+



1162
1163
1164
# File 'lib/HDLRuby/hruby_low2c.rb', line 1162

def add_blocks_code(res,level)
    self.statement.add_blocks_code(res,level)
end

#blocks2seq!Object

Converts the par sub blocks to seq.



135
136
137
138
139
# File 'lib/HDLRuby/hruby_low2seq.rb', line 135

def blocks2seq!
    # Convert the statement.
    self.statement.blocks2seq!
    return self
end

#boolean_in_assign2select!Object

Converts booleans in assignments to select operators.



93
94
95
96
97
98
99
100
# File 'lib/HDLRuby/hruby_low_bool2select.rb', line 93

def boolean_in_assign2select!
    # No need to apply on the match!
    # # Apply on the match.
    # self.set_match!(self.match.boolean_in_assign2select)
    # Apply on the statement.
    self.statement.boolean_in_assign2select!
    return self
end

#break_types!(types) ⇒ Object

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



594
595
596
597
598
599
600
601
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 594

def break_types!(types)
    self.each_node do |node|
        # Need to break only in the case of a cast.
        if node.is_a?(Cast) then
            node.type.break_types!(types)
        end
    end
end

#cloneObject

Clones the When (deeply)



2932
2933
2934
# File 'lib/HDLRuby/hruby_low.rb', line 2932

def clone
    return When.new(@match.clone,@statement.clone)
end

#delete_unless!(keep) ⇒ Object

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



145
146
147
148
# File 'lib/HDLRuby/hruby_low_cleanup.rb', line 145

def delete_unless!(keep)
    # Recurse on the statement.
    self.statement.delete_unless!(keep)
end

#each_block(&ruby_block) ⇒ Object

Iterates over the sub blocks.



2937
2938
2939
2940
2941
2942
2943
# File 'lib/HDLRuby/hruby_low.rb', line 2937

def each_block(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_block) unless ruby_block
    # A ruby block?
    # Apply it on the statement if it is a block.
    ruby_block.call(@statement) if @statement.is_a?(Block)
end

#each_block_deep(&ruby_block) ⇒ Object

Iterates over all the blocks contained in the current block.



2946
2947
2948
2949
2950
2951
2952
# File 'lib/HDLRuby/hruby_low.rb', line 2946

def each_block_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_block_deep) unless ruby_block
    # A ruby block?
    # Recurse on the statement.
    @statement.each_block_deep(&ruby_block)
end

#each_node(&ruby_block) ⇒ Object

Interates over the children.



2964
2965
2966
2967
2968
2969
2970
2971
# File 'lib/HDLRuby/hruby_low.rb', line 2964

def each_node(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_node) unless ruby_block
    # A ruby block?
    # Appy it on the children.
    ruby_block.call(@match)
    ruby_block.call(@statement)
end

#each_node_deep(&ruby_block) ⇒ Object

Iterates over the nodes deeply if any.



2974
2975
2976
2977
2978
2979
2980
2981
2982
# File 'lib/HDLRuby/hruby_low.rb', line 2974

def each_node_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_node_deep) unless ruby_block
    # A ruby block? First apply it to current.
    ruby_block.call(self)
    # And recurse on the children
    @match.each_node_deep(&ruby_block)
    @statement.each_node_deep(&ruby_block)
end

#each_statement_deep(&ruby_block) ⇒ Object

Iterates over all the stamements of the block and its sub blocks.



2955
2956
2957
2958
2959
2960
2961
# File 'lib/HDLRuby/hruby_low.rb', line 2955

def each_statement_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_statement_deep) unless ruby_block
    # A ruby block?
    # Recurse on the statement.
    @statement.each_statement_deep(&ruby_block)
end

#eql?(obj) ⇒ Boolean

Comparison for hash: structural comparison.



2919
2920
2921
2922
2923
2924
# File 'lib/HDLRuby/hruby_low.rb', line 2919

def eql?(obj)
    return false unless obj.is_a?(When)
    return false unless @match.eql?(obj.match)
    return false unless @statement.eql?(obj.statement)
    return true
end

#explicit_types!(type) ⇒ Object

Explicit the types conversions in the when where +type+ is the type of the selecting value.



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

def explicit_types!(type)
    # Recurse on the match, it must be of type.
    self.set_match!(self.match.explicit_types(type))
    # Recurse on the statement.
    self.statement.explicit_types!
    return self
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!



578
579
580
581
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 578

def extract_declares!
    # Recurse on the statement.
    return self.statement.extract_declares!
end

#extract_selects!Object

Extract the Select expressions.

NOTE: work on the match only.



159
160
161
162
163
# File 'lib/HDLRuby/hruby_low_without_select.rb', line 159

def extract_selects!
    selects = []
    self.set_match!(self.match.extract_selects_to!(selects))
    return selects
end

#hashObject

Hash function.



2927
2928
2929
# File 'lib/HDLRuby/hruby_low.rb', line 2927

def hash
    return [@match,@statement].hash
end

#map_nodes!(&ruby_block) ⇒ Object

Maps on the children (including the match).



728
729
730
731
732
733
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 728

def map_nodes!(&ruby_block)
    @match = ruby_block.call(@match)
    @match.parent = self unless @match.parent
    @statement = ruby_block.call(@statement)
    @statement.parent = self unless @statement.parent
end

#mix?(mode = nil) ⇒ Boolean

Tell if there is a mix block. +mode+ is the mode of the upper block.



143
144
145
146
# File 'lib/HDLRuby/hruby_low2seq.rb', line 143

def mix?(mode = nil)
    # Check the statement.
    return statement.mix?(mode)
end

#replace_expressions!(node2rep) ⇒ Object

Replaces sub expressions using +node2rep+ table indicating the node to replace and the corresponding replacement. Returns the actually replaced nodes and their corresponding replacement.

NOTE: the replacement is duplicated.



741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 741

def replace_expressions!(node2rep)
    # First recurse on the children.
    res = {}
    self.each_node do |node|
        res.merge!(node.replace_expressions!(node2rep))
    end
    # Is there a replacement to do on the value?
    rep = node2rep[self.match]
    if rep then
        # Yes, do it.
        rep = rep.clone
        node = self.match
        # node.set_parent!(nil)
        self.set_match!(rep)
        # And register the replacement.
        res[node] = rep
    end

    return res
end

#replace_names!(former, nname) ⇒ Object

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



584
585
586
587
588
589
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 584

def replace_names!(former,nname)
    # Recurse on the match.
    self.match.replace_names!(former,nname)
    # Recurse on the statement.
    self.statement.replace_names!(former,nname)
end

#set_match!(match) ⇒ Object

Sets the match.



703
704
705
706
707
708
709
710
711
712
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 703

def set_match!(match)
    # Checks the match.
    unless match.is_a?(Expression)
        raise AnyError, "Invalid class for a case match: #{match.class}"
    end
    # Set the match.
    @match = match
    # And set their parents.
    match.parent = self
end

#set_statement!(statement) ⇒ Object

Sets the statement.



715
716
717
718
719
720
721
722
723
724
725
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 715

def set_statement!(statement)
    # Checks statement.
    unless statement.is_a?(Statement)
        raise AnyError,
              "Invalid class for a statement: #{statement.class}"
    end
    # Set the statement.
    @statement = statement
    # And set their parents.
    statement.parent = self
end

#to_c(level = 0) ⇒ Object

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



1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
# File 'lib/HDLRuby/hruby_low2c.rb', line 1147

def to_c(level = 0)
    # The result string.
    res = " " * level*3
    # Generate the match.
    res << "case " << self.match.to_c(level+1) << ": {\n"
    # Generate the statement.
    res << self.statement.to_c(level+1)
    # Adds a break
    res << " " * (level+1)*3 << "break;\n"
    res << " " * level*3 << "}\n"
    # Returns the result.
    return res
end

#to_high(level = 0) ⇒ Object

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



381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/HDLRuby/hruby_low2high.rb', line 381

def to_high(level = 0)
    # The result string.
    res = " " * (level*3)
    # Generate the match.
    res << "hwhen " << self.match.to_high(level+1) << " do\n"
    # Generate the statement.
    res << self.statement.to_high(level+1)
    # Close the when.
    res << " " * (level*3) << "end\n"
    # Returns the result.
    return res
end

#to_upper_space!Object

Moves the declarations to the upper namespace.



570
571
572
573
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 570

def to_upper_space!
    # Recurse on the statement.
    self.statement.to_upper_space!
end

#to_vhdl(vars, type, level = 0) ⇒ Object

Generates the text of the equivalent HDLRuby::High code ensuring the match is of +type+. +vars+ is the list of the variables and +level+ is the hierachical level of the object.



916
917
918
919
920
921
922
923
924
925
# File 'lib/HDLRuby/hruby_low2vhd.rb', line 916

def to_vhdl(vars,type,level = 0)
    # The result string.
    res = " " * (level*3)
    # Generate the match.
    res << "when " << Low2VHDL.to_type(type,self.match) << " =>\n"
    # Generate the statement.
    res << self.statement.to_vhdl(vars,level+1)
    # Returns the result.
    return res
end

#top_blockObject

Gets the top block, i.e. the first block of the current behavior.



2985
2986
2987
# File 'lib/HDLRuby/hruby_low.rb', line 2985

def top_block
    return self.parent.is_a?(Behavior) ? self : self.parent.top_block
end

#with_var(upper = nil) ⇒ Object

Converts to a variable-compatible case where +upper+ is the upper block if any.

NOTE: the result is a new case.



284
285
286
# File 'lib/HDLRuby/hruby_low_with_var.rb', line 284

def with_var(upper = nil)
    return When.new(self.match.clone,self.statement.with_var(upper))
end