class HDLRuby::Low::Concat
Describes a concatenation expression.
Describes a concatenation expression.
Extends the Concat
class with generation of C text.
Extends the Concat
class with generation of hdr text.
Add the conversion to high.
Extends the Concat
class with generation of HDLRuby::High
text.
Extends the Concat
class with functionality for converting booleans in assignments to select operators.
Extends the Concat
class with functionality for converting booleans in assignments to select operators.
Extends the Concat
class with fixing of types and constants.
Describes a concatenation expression.
Use it when collecting.
Public Class Methods
Creates a new concatenation with type
of several expressions
together.
def initialize(expressions = [])
# File lib/HDLRuby/hruby_low.rb, line 5046 def initialize(type,expressions = []) super(type) # Initialize the array of expressions that are concatenated. @expressions = [] # Check and add the expressions. expressions.each { |expression| self.add_expression(expression) } end
Public Instance Methods
Adds an expression
to concat.
# File lib/HDLRuby/hruby_low.rb, line 5099 def add_expression(expression) # Check expression. unless expression.is_a?(Expression) then raise AnyError, "Invalid class for an expression: #{expression.class}" end # Add it. @expressions << expression # And set its parent. expression.parent = self expression end
Converts booleans in assignments to select operators.
# File lib/HDLRuby/hruby_low_bool2select.rb, line 261 def boolean_in_assign2select # Recurse on the sub expressions. return Concat.new(self.type,self.each_expression.map do |expr| expr.boolean_in_assign2select end ) end
Extracts the expressions from the casts.
# File lib/HDLRuby/hruby_low_casts_without_expression.rb, line 269 def casts_without_expression # Recurse on the sub expressions. return Concat.new(self.type,self.each_expression.map do |expr| expr.casts_without_expression end ) end
Clones the concatenated expression (deeply)
# File lib/HDLRuby/hruby_low.rb, line 5142 def clone return Concat.new(@type, @expressions.map {|expr| expr.clone } ) end
Delete an expression.
# File lib/HDLRuby/hruby_low_mutable.rb, line 1676 def delete_expression!(expression) if @expressions.include?(expression) then # The expression is present, delete it. @expressions.delete(expression) # And remove its parent. expression.parent = nil end expression end
Iterates over each object deeply.
Returns an enumerator if no ruby block is given.
# File lib/HDLRuby/hruby_low.rb, line 5065 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) # Then apply on the type. self.type.each_deep(&ruby_block) # Then apply on the expressions. self.each_expression do |expr| expr.each_deep(&ruby_block) end end
Iterates over the concatenated expressions.
Returns an enumerator if no ruby block is given.
# File lib/HDLRuby/hruby_low.rb, line 5115 def each_expression(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each_expression) unless ruby_block # A ruby block? Apply it on each children. @expressions.each(&ruby_block) end
Iterates over the nodes deeply if any.
# File lib/HDLRuby/hruby_low.rb, line 5124 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. self.each_expression do |expr| expr.each_node_deep(&ruby_block) end end
Comparison for hash: structural comparison.
# File lib/HDLRuby/hruby_low.rb, line 5079 def eql?(obj) # General comparison. return false unless super(obj) # Specific comparison. return false unless obj.is_a?(Concat) idx = 0 obj.each_expression do |expression| return false unless @expressions[idx].eql?(expression) idx += 1 end return false unless idx == @expressions.size return true end
Explicit the types conversions in the concat where type
is the expected type of the condition if any.
# File lib/HDLRuby/hruby_low_fix_types.rb, line 307 def explicit_types(type = nil) # Is there a type to match? if type then # Yes, update the concat to the type. # Get the real type in case of typedef. type = type.def while type.is_a?(TypeDef) # Is it an array type? if type.is_a?(TypeVector) then # Yes, update the concat without subcasting. return Concat.new(type,self.each_expression.map do |expr| expr.explicit_types end) else # No, it should be a tuple. return Concat.new(type, self.each_expression.map.with_index do |expr,i| expr.explicit_types(type.get_type(i)) end) end else # No, recurse on the sub expressions. return Concat.new(self.type, self.each_expression.map do |expr| expr.explicit_types end) end end
Hash
function.
# File lib/HDLRuby/hruby_low.rb, line 5094 def hash return [super,@expressions].hash end
Tells if the expression is immutable (cannot be written.)
# File lib/HDLRuby/hruby_low.rb, line 5055 def immutable? # Immutable if children are all immutable. return self.each_expression.reduce(true) do |r,c| r && c.immutable? end end
Maps on the expression.
# File lib/HDLRuby/hruby_low_mutable.rb, line 1665 def map_expressions!(&ruby_block) @expressions.map! do |expression| expression = ruby_block.call(expression) expression.parent = self unless expression.parent expression end end
Generates the C text of the equivalent HDLRuby
code. level
is the hierachical level of the object.
# File lib/HDLRuby/hruby_low2c.rb, line 1825 def to_c(level = 0) # Gather the content to concat. expressions = self.each_expression.to_a # Create the resulting string. # res = " " * (level*3) res = "({\n" # Overrides the upper src0, src1, ..., and dst... # And allocates a new value for dst. res << (" " * ((level+1)*3)) res << "Value #{expressions.size.times.map do |i| "src#{i}" end.join(",")};\n" res << (" " * ((level+1)*3)) res << "Value dst = get_value();\n" # Save the state of the value pool. res << (" " * ((level+1)*3)) res << "unsigned int pool_state = get_value_pos();\n" # Compute each sub expression. expressions.each_with_index do |expr,i| res << (" " * ((level+1)*3)) res << "src#{i} = #{expr.to_c(level+2)};\n" end # Compute the direction. # Compute the resulting concatenation. res << (" " * ((level+1)*3)) res << "concat_value(#{expressions.size}," res << "#{self.type.direction == :little ? 1 : 0},dst," res << "#{expressions.size.times.map { |i| "src#{i}" }.join(",")}" res << ");\n" # Restore the state of the value pool. res << (" " * ((level+1)*3)) res << "set_value_pos(pool_state);\n" # Close the computation. res << (" " * (level*3)) res << "dst; })" return res end
Generates the text of the equivalent hdr text. level
is the hierachical level of the object.
# File lib/HDLRuby/hruby_low2hdr.rb, line 631 def to_hdr(level = 0) # The resulting string. res = "" # Generate the header. res << "[ " # Generate the expressions. res << self.each_expression.map do |expression| expression.to_hdr(level+1) end.join(", ") # Close the select. res << " ]" # Return the resulting string. return res end
Creates a new high concat expression.
# File lib/HDLRuby/hruby_low2high.rb, line 448 def to_high return HDLRuby::High::Concat.new(self.type.to_high, self.each_expression.map { |ex| ex.to_high }) end
# File lib/HDLRuby/hruby_verilog.rb, line 1704 def to_verilog expression = self.each_expression.to_a result = "{" expression[0..-2].each do |expression| result << "#{expression.to_verilog}," end result << "#{expression.last.to_verilog}}" return result end
Generates the text of the equivalent HDLRuby::High
code. type
is the expected type of the content. level
is the hierachical level of the object. def to_vhdl
(type,level = 0)
# File lib/HDLRuby/hruby_low2vhd.rb, line 1375 def to_vhdl(level = 0, type = self.type) raise "Invalid class for a type: #{type.class}" unless type.is_a?(Type) # The resulting string. res = "" # Generate the header. # Generate the expressions. # Depends if it is an initialization or not. # if self.type.is_a?(TypeTuple) then if self.parent.is_a?(SignalC) then res << "( " << self.each_expression.map do |expression| Low2VHDL.to_type(type,expression) end.join(",\n#{" "*((level+1)*3)}") << " )" else # Compute the width of the concatenation. width = self.each_expression.reduce(0) do |sum,expr| sum += expr.type.width end # Generate the missing bits if any. width = type.width - width res << '"' + "0" * width + '" & ' if width > 0 # Generate the concatenation. res << self.each_expression.map do |expression| # "(" + Low2VHDL.to_type(type,expression) + ")" "(" + expression.to_vhdl(level+1) + ")" end.join(" & ") end # Return the resulting string. return res end
Tell if the expression includes a signal whose name is one of names
.
# File lib/HDLRuby/hruby_low.rb, line 5136 def use_name?(*names) # Recurse on the expressions. return @expressions.any? { |expr| expr.use_name?(*names) } end