Class: HDLRuby::Low::TypeTuple

Inherits:
Type
  • Object
show all
Defined in:
lib/HDLRuby/hruby_low.rb,
lib/HDLRuby/hruby_viz.rb,
lib/HDLRuby/hruby_low2c.rb,
lib/HDLRuby/hruby_low2hdr.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

Describes a tuple type.

Direct Known Subclasses

High::TypeTuple

Constant Summary

Constants included from Low2Symbol

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

Instance Attribute Summary

Attributes inherited from Type

#name

Attributes included from Hparent

#parent

Instance Method Summary collapse

Methods inherited from Type

#boolean?, #fixed?, #float?, #hierarchical?, #leaf?, #max, #min, #range?, #set_name!, #signed?, #struct?, #to_vector, #to_verilog, #unsigned?, #vector?

Methods included from Low2Symbol

#to_sym

Methods included from Hparent

#absolute_ref, #hierarchy, #no_parent!, #scope

Constructor Details

#initialize(name, direction, *content) ⇒ TypeTuple

Creates a new tuple type named name width direction and whose sub types are given by content.



2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
# File 'lib/HDLRuby/hruby_low.rb', line 2028

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.



2084
2085
2086
2087
2088
2089
2090
# File 'lib/HDLRuby/hruby_low.rb', line 2084

def add_type(type)
    unless type.is_a?(Type) then
        raise AnyError, 
              "Invalid class for a type: #{type.class} (#{type})"
    end
    @types << type
end

#baseObject

Gets the base type.

NOTE: only valid if the tuple is regular (i.e., all its sub types are identical)



2171
2172
2173
2174
2175
2176
2177
2178
# File 'lib/HDLRuby/hruby_low.rb', line 2171

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)

Returns:

  • (Boolean)


2163
2164
2165
# File 'lib/HDLRuby/hruby_low.rb', line 2163

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.



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 357

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.



335
336
337
338
339
340
341
342
343
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 335

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

#directionObject

Get the direction of the type, little or big endian.



2142
2143
2144
# File 'lib/HDLRuby/hruby_low.rb', line 2142

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.



2095
2096
2097
2098
2099
2100
# File 'lib/HDLRuby/hruby_low.rb', line 2095

def each(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each) unless ruby_block
    # A ruby block? Apply it on each sub name/type pair.
    @types.each(&ruby_block)
end

#each_type(&ruby_block) ⇒ Object

Iterates over the sub types.

Returns an enumerator if no ruby block is given.



2105
2106
2107
2108
2109
2110
# File 'lib/HDLRuby/hruby_low.rb', line 2105

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 sub type.
    @types.each(&ruby_block)
end

#each_type_deep(&ruby_block) ⇒ Object Also known as: each_deep

Iterates over the types deeply if any.



2113
2114
2115
2116
2117
2118
2119
2120
# File 'lib/HDLRuby/hruby_low.rb', line 2113

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.

Returns:

  • (Boolean)


2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
# File 'lib/HDLRuby/hruby_low.rb', line 2049

def eql?(obj)
    # # General type comparison.
    # return false unless super(obj)
    return false unless obj.is_a?(TypeTuple)
    # 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.

Returns:

  • (Boolean)


2184
2185
2186
2187
# File 'lib/HDLRuby/hruby_low.rb', line 2184

def equivalent?(type)
    return (type.is_a?(TypeTuple) and
            !@types.zip(type.types).index {|t0,t1| !t0.equivalent?(t1) })
end

#get_all_typesObject

Gets an array containing all the syb types.



2074
2075
2076
# File 'lib/HDLRuby/hruby_low.rb', line 2074

def get_all_types
    return @types.clone
end

#get_type(index) ⇒ Object

Gets a sub type by index.



2079
2080
2081
# File 'lib/HDLRuby/hruby_low.rb', line 2079

def get_type(index)
    return @types[index.to_i]
end

#hashObject

Hash function.



2064
2065
2066
# File 'lib/HDLRuby/hruby_low.rb', line 2064

def hash
    return [super,@types].hash
end

#map_types!(&ruby_block) ⇒ Object

Maps on the sub types.



330
331
332
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 330

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

#rangeObject

Gets the range of the type.

NOTE: only valid if the tuple is regular (i.e., all its sub types are identical)



2150
2151
2152
2153
2154
2155
2156
2157
# File 'lib/HDLRuby/hruby_low.rb', line 2150

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.

Returns:

  • (Boolean)


2127
2128
2129
2130
2131
2132
2133
2134
# File 'lib/HDLRuby/hruby_low.rb', line 2127

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(res, level = 0) ⇒ Object

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

NOTE: type tuples are converted to bit vector of their contents. def to_c(level = 0)



632
633
634
635
636
# File 'lib/HDLRuby/hruby_low2c.rb', line 632

def to_c(res,level = 0)
    # return self.to_vector.to_c(level)
    self.to_vector.to_c(res,level)
    return res
end

#to_hdr(level = 0) ⇒ Object

Generates the text of the equivalent hdr text. level is the hierachical level of the object.



216
217
218
219
220
221
222
223
224
225
# File 'lib/HDLRuby/hruby_low2hdr.rb', line 216

def to_hdr(level = 0)
    # The resulting string.
    res = "["
    # Generate each sub type.
    res << self.each_type.map { |type| type.to_hdr(level) }.join(", ")
    # Close the tuple.
    res << "]"
    # Return the result.
    return res
end

#to_highObject

Creates a new high type tuple.



117
118
119
120
# File 'lib/HDLRuby/hruby_low2high.rb', line 117

def to_high
    return HDLRuby::High::TypeTuple.new(self.name,self.direction,
                        *self.each_type.map { |typ| typ.to_high })
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.



716
717
718
719
# File 'lib/HDLRuby/hruby_low2vhd.rb', line 716

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

#to_viz_nameObject

Convert to a name usable in a Viz representation.



5049
5050
5051
# File 'lib/HDLRuby/hruby_viz.rb', line 5049

def to_viz_name
  return "["+ self.each_type.map {|sub| sub.to_viz_name }.join(",") +"]"
end

#types?Boolean

Tells if the type has sub types.

Returns:

  • (Boolean)


2069
2070
2071
# File 'lib/HDLRuby/hruby_low.rb', line 2069

def types?
    return true
end

#widthObject

Gets the bitwidth.



2137
2138
2139
# File 'lib/HDLRuby/hruby_low.rb', line 2137

def width
    return @types.reduce(0) { |sum,type| sum + type.width }
end