class HDLRuby::Low::Unary

Describes an unary operation.

Describes an unary operation.

Extends the Unary class with generation of C text.

Extends the Unary class with generation of hdr text.

Add the conversion to high.

Extends the Unary class with generation of HDLRuby::High text.

Extends the Unary class with functionality for converting booleans in assignments to select operators.

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

Extends the Unary class with fixing of types and constants.

Describes an unary operation.

Extends the Unary class for checking if it is a boolean expression or not.

Used when using “~” for expressions.

Attributes

child[R]

The child.

Public Class Methods

new(type,operator,child) click to toggle source

Creates a new unary expression with type applying operator on child expression. def initialize(operator,child)

Calls superclass method
# File lib/HDLRuby/hruby_low.rb, line 4673
def initialize(type,operator,child)
    # Initialize as a general operation.
    super(type,operator)
    # Check and set the child.
    unless child.is_a?(Expression)
        raise AnyError,
              "Invalid class for an expression: #{child.class}"
    end
    @child = child
    # And set its parent.
    child.parent = self
end

Public Instance Methods

boolean?() click to toggle source

Tells if the expression is boolean.

# File lib/HDLRuby/hruby_low_with_bool.rb, line 107
def boolean?
    return self.child.boolean?
end
boolean_in_assign2select() click to toggle source

Converts booleans in assignments to select operators.

# File lib/HDLRuby/hruby_low_bool2select.rb, line 197
def boolean_in_assign2select
    # Recurse on the sub node.
    return Unary.new(self.type,self.operator,
                     self.child.boolean_in_assign2select)
    return self
end
casts_without_expression() click to toggle source

Extracts the expressions from the casts.

# File lib/HDLRuby/hruby_low_casts_without_expression.rb, line 224
def casts_without_expression
    # Recurse on the sub node.
    return Unary.new(self.type,self.operator,
                     self.child.casts_without_expression)
    return self
end
clone() click to toggle source

Clones the unary operator (deeply)

# File lib/HDLRuby/hruby_low.rb, line 4760
def clone
    return Unary.new(@type,self.operator,@child.clone)
end
each_deep(&ruby_block) click to toggle source

Iterates over each object deeply.

Returns an enumerator if no ruby block is given.

# File lib/HDLRuby/hruby_low.rb, line 4695
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 child.
    self.child.each_deep(&ruby_block)
end
each_expression(&ruby_block)
Alias for: each_node
each_node(&ruby_block) click to toggle source

Iterates over the expression children if any.

# File lib/HDLRuby/hruby_low.rb, line 4722
def each_node(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_node) unless ruby_block
    # A ruby block? Apply it on the child.
    ruby_block.call(@child)
end
Also aliased as: each_expression
each_node_deep(&ruby_block) click to toggle source

Iterates over the nodes deeply if any.

# File lib/HDLRuby/hruby_low.rb, line 4732
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 child.
    @child.each_node_deep(&ruby_block)
end
each_ref_deep(&ruby_block) click to toggle source

Iterates over all the references encountered in the expression.

NOTE: do not iterate inside the references.

# File lib/HDLRuby/hruby_low.rb, line 4744
def each_ref_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_ref_deep) unless ruby_block
    # puts "each_ref_deep for Unary"
    # A ruby block?
    # Recurse on the child.
    @child.each_ref_deep(&ruby_block)
end
eql?(obj) click to toggle source

Comparison for hash: structural comparison.

Calls superclass method
# File lib/HDLRuby/hruby_low.rb, line 4707
def eql?(obj)
    # General comparison.
    return false unless super(obj)
    # Specific comparison.
    return false unless obj.is_a?(Unary)
    return false unless @child.eql?(obj.child)
    return true
end
explicit_types(type = nil) click to toggle source

Explicit the types conversions in the unary operation where type is the expected type of the condition if any.

# File lib/HDLRuby/hruby_low_fix_types.rb, line 249
def explicit_types(type = nil)
    # Recurse on the child (no type to specify here, unary operations
    # preserve the type of their child).
    op = Unary.new(self.type,self.operator,self.child.explicit_types)
    # Does the type match the operation?
    if type && !self.type.eql?(type) then
        # No create a cast.
        return Cast.new(type,op)
    else
        # Yes, return the operation as is.
        return op
    end
end
hash() click to toggle source

Hash function.

Calls superclass method
# File lib/HDLRuby/hruby_low.rb, line 4717
def hash
    return [super,@child].hash
end
immutable?() click to toggle source

Tells if the expression is immutable (cannot be written.)

# File lib/HDLRuby/hruby_low.rb, line 4687
def immutable?
    # Immutable if the child is immutable.
    return child.immutable?
end
to_c(level = 0) click to toggle source

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

# File lib/HDLRuby/hruby_low2c.rb, line 1654
def to_c(level = 0)
    res = "({\n"
    # Overrides the upper src0 and dst...
    res << (" " * ((level+1)*3))
    res << "Value src0, dst;\n"
    if (self.operator != :+@) then
        # And allocates a new value for dst unless the operator
        # is +@ that does not compute anything.
        res << (" " * ((level+1)*3))
        res << "dst = get_value();\n"
    end
    # Save the state of the value pool.
    res << (" " * ((level+1)*3))
    res << "unsigned int pool_state = get_value_pos();\n"
    # Compute the child.
    res << (" " * ((level+1)*3))
    res << "src0 = #{self.child.to_c(level+2)};\n"
    res << (" " * ((level+1)*3))
    case self.operator
    when :~ then
        res += "dst = not_value(src0,dst);\n"
    when :-@ then
        res += "dst = neg_value(src0,dst);\n"
    when :+@ then
        res += "dst = #{self.child.to_c(level)};\n"
    else
        raise "Invalid unary operator: #{self.operator}."
    end
    # Restore the value pool state.
    res << (" " * ((level+1)*3))
    res << "set_value_pos(pool_state);\n"
    # Close the computation
    res << (" " * (level*3))
    res << "dst; })"

    return res
end
to_hdr(level = 0) click to toggle source

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

# File lib/HDLRuby/hruby_low2hdr.rb, line 589
def to_hdr(level = 0)
    return "(#{self.operator.to_s[0]}" + self.child.to_hdr(level) + ")"
end
to_high() click to toggle source

Creates a new high unary expression.

# File lib/HDLRuby/hruby_low2high.rb, line 416
def to_high
    return HDLRuby::High::Unary.new(self.type.to_high,self.operator,
                                    self.child.to_high)
end
to_verilog() click to toggle source

Converts the system to Verilog code.

# File lib/HDLRuby/hruby_verilog.rb, line 1629
def to_verilog
    return "#{self.operator[0]}#{self.child.to_verilog}"
end
to_vhdl(level = 0, std_logic = false) click to toggle source

Generates the text of the equivalent HDLRuby::High code. level is the hierachical level of the object. std_logic tells if std_logic computation is to be done.

# File lib/HDLRuby/hruby_low2vhd.rb, line 1248
def to_vhdl(level = 0, std_logic = false)
    # Generate the operator string.
    operator = self.operator == :~ ? "not " : self.operator.to_s[0]
    # Is the operator arithmetic?
    if [:+@, :-@].include?(self.operator) then
        # Yes, type conversion my be required by VHDL standard.
        res = "#{Low2VHDL.unarith_cast(self)}(#{operator}" +
                     Low2VHDL.to_arith(self.child) + ")"
        res += "(0)" if std_logic
        return res
    else
        # No, generate simply the unary operation.
        # (The other unary operator is logic, no need to force
        # std_logic.)
        return "(#{operator}" + self.child.to_vhdl(level,std_logic) + ")"
    end
end
use_name?(*names) click to toggle source

Tell if the expression includes a signal whose name is one of names.

# File lib/HDLRuby/hruby_low.rb, line 4754
def use_name?(*names)
    # Recurse on the child.
    return @child.use_name?(*names)
end