Class: HDLRuby::Low::TypeTuple
- Includes:
- Ltype
- Defined in:
- lib/HDLRuby/hruby_db.rb,
lib/HDLRuby/hruby_low.rb,
lib/HDLRuby/hruby_low2c.rb,
lib/HDLRuby/hruby_low2vhd.rb,
lib/HDLRuby/hruby_low2high.rb,
lib/HDLRuby/hruby_low_mutable.rb,
lib/HDLRuby/hruby_low_skeleton.rb,
lib/HDLRuby/hruby_low_without_namespace.rb
Overview
Extends the TypeTuple class with functionality for breaking hierarchical types.
Direct Known Subclasses
Constant Summary
Constants included from Low2Symbol
Low2Symbol::Low2SymbolPrefix, Low2Symbol::Low2SymbolTable, Low2Symbol::Symbol2LowTable
Instance Attribute Summary
Attributes inherited from Type
Attributes included from Hparent
Instance Method Summary collapse
-
#add_type(type) ⇒ Object
Adds a sub +type+.
-
#base ⇒ Object
Gets the base type.
-
#base? ⇒ Boolean
Tells if the type has a base.
-
#break_types!(types) ⇒ Object
Breaks the hierarchical types into sequences of type definitions.
-
#delete_type!(type) ⇒ Object
Deletes a type.
-
#direction ⇒ Object
Get the direction of the type, little or big endian.
-
#each(&ruby_block) ⇒ Object
Iterates over the sub name/type pair.
-
#each_type(&ruby_block) ⇒ Object
Iterates over the sub types.
-
#each_type_deep(&ruby_block) ⇒ Object
Iterates over the types deeply if any.
-
#eql?(obj) ⇒ Boolean
Comparison for hash: structural comparison.
-
#equivalent?(type) ⇒ Boolean
Tell if +type+ is equivalent to current type.
-
#get_all_types ⇒ Object
Gets an array containing all the syb types.
-
#get_type(index) ⇒ Object
Gets a sub type by +index+.
-
#hash ⇒ Object
Hash function.
-
#initialize(name, direction, *content) ⇒ TypeTuple
constructor
Creates a new tuple type named +name+ width +direction+ and whose sub types are given by +content+.
-
#map_types!(&ruby_block) ⇒ Object
Maps on the sub types.
-
#range ⇒ Object
Gets the range of the type.
-
#regular? ⇒ Boolean
Tell if the tuple is regular, i.e., all its sub types are equivalent.
-
#to_c(level = 0) ⇒ Object
Generates the text of the equivalent HDLRuby::High code.
-
#to_high(level = 0) ⇒ Object
Generates the text of the equivalent HDLRuby::High code.
-
#to_vhdl(level = 0) ⇒ Object
Generates the text of the equivalent HDLRuby::High code.
-
#types? ⇒ Boolean
Tells if the type has sub types.
-
#width ⇒ Object
Gets the bitwidth.
Methods included from Ltype
Methods inherited from Type
#boolean?, #fixed?, #float?, #leaf?, #range?, #set_name!, #signed?, #struct?, #to_vector, #to_verilog, #unsigned?
Methods included from Low2Symbol
Methods included from Hparent
Constructor Details
#initialize(name, direction, *content) ⇒ TypeTuple
Creates a new tuple type named +name+ width +direction+ and whose sub types are given by +content+.
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 |
# File 'lib/HDLRuby/hruby_low.rb', line 1637 def initialize(name,direction,*content) # Initialize the type. super(name) # Set the direction. @direction = direction.to_sym unless [:little, :big].include?(@direction) raise AnyError, "Invalid direction for a type: #{direction}" end # Check and set the content. content.each do |sub| unless sub.is_a?(Type) then raise AnyError, "Invalid class for a type: #{sub.class}" end end @types = content end |
Instance Method Details
#add_type(type) ⇒ Object
Adds a sub +type+.
1692 1693 1694 1695 1696 1697 1698 |
# File 'lib/HDLRuby/hruby_low.rb', line 1692 def add_type(type) unless type.is_a?(Type) then raise AnyError, "Invalid class for a type: #{type.class} (#{type})" end @types << type end |
#base ⇒ Object
Gets the base type.
NOTE: only valid if the tuple is regular (i.e., all its sub types are identical)
1777 1778 1779 1780 1781 1782 1783 1784 |
# File 'lib/HDLRuby/hruby_low.rb', line 1777 def base if regular? then # Regular tuple, return the type of its first element. return @types[0] else raise AnyError, "No base type for type #{self}" end end |
#base? ⇒ Boolean
Tells if the type has a base.
NOTE: only if the tuple is regular (i.e., all its sub types are identical)
1769 1770 1771 |
# File 'lib/HDLRuby/hruby_low.rb', line 1769 def base? return regular? 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.
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 337 def break_types!(types) self.map_types! do |sub| if sub.is_a?(TypeVector) || sub.is_a?(TypeTuple) || sub.is_a?(TypeStruct) then # Need to break # First recurse on the sub. nsub = sub.break_types!(types) # Maybe such a type already exists. ndef = types[sub] if ndef then # Yes, use it. ndef.clone else # No change it to a type definition ndef = TypeDef.new(HDLRuby.uniq_name,nsub) # And add it to the types by structure. types[nsub] = ndef nsub end end end return self end |
#delete_type!(type) ⇒ Object
Deletes a type.
307 308 309 310 311 312 313 314 315 |
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 307 def delete_type!(type) if @types.include?(type) then # The type is present, delete it. @types.delete(type) # And remove its parent. type.parent = nil end type end |
#direction ⇒ Object
Get the direction of the type, little or big endian.
1748 1749 1750 |
# File 'lib/HDLRuby/hruby_low.rb', line 1748 def direction return @direction end |
#each(&ruby_block) ⇒ Object
Iterates over the sub name/type pair.
Returns an enumerator if no ruby block is given.
1703 1704 1705 1706 1707 1708 |
# File 'lib/HDLRuby/hruby_low.rb', line 1703 def each(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each) unless ruby_block # A ruby block? Apply it on each input signal instance. @types.each(&ruby_block) end |
#each_type(&ruby_block) ⇒ Object
Iterates over the sub types.
Returns an enumerator if no ruby block is given.
1713 1714 1715 1716 1717 1718 |
# File 'lib/HDLRuby/hruby_low.rb', line 1713 def each_type(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each_type) unless ruby_block # A ruby block? Apply it on each input signal instance. @types.each(&ruby_block) end |
#each_type_deep(&ruby_block) ⇒ Object
Iterates over the types deeply if any.
1721 1722 1723 1724 1725 1726 1727 1728 |
# File 'lib/HDLRuby/hruby_low.rb', line 1721 def each_type_deep(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each_type_deep) unless ruby_block # A ruby block? First apply it to current. ruby_block.call(self) # And recurse on the sub types. @types.each { |type| type.each_type_deep(&ruby_block) } end |
#eql?(obj) ⇒ Boolean
Comparison for hash: structural comparison.
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 |
# File 'lib/HDLRuby/hruby_low.rb', line 1658 def eql?(obj) # General type comparison. return false unless super(obj) # Specific comparison. idx = 0 obj.each_type do |type| return false unless @types[idx].eql?(type) idx += 1 end return false unless idx == @types.size return true end |
#equivalent?(type) ⇒ Boolean
Tell if +type+ is equivalent to current type.
NOTE: type can be compatible while not being equivalent, please
refer to hruby_types.rb for type compatibility.
1790 1791 1792 1793 |
# File 'lib/HDLRuby/hruby_low.rb', line 1790 def equivalent?(type) return (type.is_a?(TypeTuple) and !@types.zip(type.types).index {|t0,t1| !t0.equivalent?(t1) }) end |
#get_all_types ⇒ Object
Gets an array containing all the syb types.
1682 1683 1684 |
# File 'lib/HDLRuby/hruby_low.rb', line 1682 def get_all_types return @types.clone end |
#get_type(index) ⇒ Object
Gets a sub type by +index+.
1687 1688 1689 |
# File 'lib/HDLRuby/hruby_low.rb', line 1687 def get_type(index) return @types[index.to_i] end |
#hash ⇒ Object
Hash function.
1672 1673 1674 |
# File 'lib/HDLRuby/hruby_low.rb', line 1672 def hash return [super,@types].hash end |
#map_types!(&ruby_block) ⇒ Object
Maps on the sub types.
302 303 304 |
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 302 def map_types!(&ruby_block) @types.map(&ruby_block) end |
#range ⇒ Object
Gets the range of the type.
NOTE: only valid if the tuple is regular (i.e., all its sub types are identical)
1756 1757 1758 1759 1760 1761 1762 1763 |
# File 'lib/HDLRuby/hruby_low.rb', line 1756 def range if regular? then # Regular tuple, return its range as if it was an array. return 0..@types.size-1 else raise AnyError, "No range for type #{self}" end end |
#regular? ⇒ Boolean
Tell if the tuple is regular, i.e., all its sub types are equivalent.
NOTE: empty tuples are assumed not to be regular.
1733 1734 1735 1736 1737 1738 1739 1740 |
# File 'lib/HDLRuby/hruby_low.rb', line 1733 def regular? return false if @types.empty? t0 = @types[0] @types[1..-1].each do |type| return false unless t0.equivalent?(type) end return true end |
#to_c(level = 0) ⇒ Object
Generates the text of the equivalent HDLRuby::High code. +level+ is the hierachical level of the object.
NOTE: type tuples are converted to bit vector of their contents.
546 547 548 549 550 |
# File 'lib/HDLRuby/hruby_low2c.rb', line 546 def to_c(level = 0) return "get_type_tuple(#{self.each.join(",") do |type| type.to_c(level+1) end})" end |
#to_high(level = 0) ⇒ Object
Generates the text of the equivalent HDLRuby::High code. +level+ is the hierachical level of the object.
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/HDLRuby/hruby_low2high.rb', line 213 def to_high(level = 0) # The resulting string. res = "[" # Generate each sub type. res << self.each_type.map { |type| type.to_high(level) }.join(", ") # Close the tuple. res << "]" # Return the result. return res end |
#to_vhdl(level = 0) ⇒ Object
Generates the text of the equivalent HDLRuby::High code. +level+ is the hierachical level of the object.
NOTE: type tuples are converted to bit vector of their contents.
690 691 692 693 |
# File 'lib/HDLRuby/hruby_low2vhd.rb', line 690 def to_vhdl(level = 0) # raise AnyError, "Tuple types are not supported in VHDL, please convert them to Struct types using Low::tuple2struct from HDLRuby/hruby_low_witout_tuple." return self.to_vector.to_vhdl(level) end |
#types? ⇒ Boolean
Tells if the type has sub types.
1677 1678 1679 |
# File 'lib/HDLRuby/hruby_low.rb', line 1677 def types? return true end |
#width ⇒ Object
Gets the bitwidth.
1743 1744 1745 |
# File 'lib/HDLRuby/hruby_low.rb', line 1743 def width return @types.reduce(0) { |sum,type| sum + type.width } end |