Class: HDLRuby::Low::Unary

Inherits:
Operation show all
Includes:
OneChildMutable
Defined in:
lib/HDLRuby/hruby_db.rb,
lib/HDLRuby/hruby_low.rb,
lib/HDLRuby/hruby_low2c.rb,
lib/HDLRuby/hruby_low2hdr.rb,
lib/HDLRuby/hruby_low2vhd.rb,
lib/HDLRuby/hruby_verilog.rb,
lib/HDLRuby/hruby_low2high.rb,
lib/HDLRuby/hruby_low_mutable.rb,
lib/HDLRuby/hruby_low_skeleton.rb,
lib/HDLRuby/hruby_low_fix_types.rb,
lib/HDLRuby/hruby_low_with_bool.rb,
lib/HDLRuby/hruby_low_bool2select.rb,
lib/HDLRuby/hruby_low_casts_without_expression.rb

Overview

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

Direct Known Subclasses

High::Unary

Constant Summary

Constants included from Low2Symbol

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

Instance Attribute Summary collapse

Attributes inherited from Operation

#operator

Attributes inherited from Expression

#type

Attributes included from Hparent

#parent

Instance Method Summary collapse

Methods included from OneChildMutable

#map_nodes!, #replace_expressions!, #set_child!

Methods inherited from Operation

#set_operator!

Methods inherited from Expression

#break_types!, #extract_selects_to!, #leftvalue?, #map_nodes!, #replace_expressions!, #replace_names!, #rightvalue?, #set_type!, #statement, #to_c_expr

Methods included from Low2Symbol

#to_sym

Methods included from Hparent

#hierarchy, #no_parent!, #scope

Constructor Details

#initialize(type, operator, child) ⇒ Unary

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



4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
# File 'lib/HDLRuby/hruby_low.rb', line 4929

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

Instance Attribute Details

#childObject (readonly)

The child.



4924
4925
4926
# File 'lib/HDLRuby/hruby_low.rb', line 4924

def child
  @child
end

Instance Method Details

#boolean?Boolean

Tells if the expression is boolean.

Returns:

  • (Boolean)


107
108
109
# File 'lib/HDLRuby/hruby_low_with_bool.rb', line 107

def boolean?
    return self.child.boolean?
end

#boolean_in_assign2selectObject

Converts booleans in assignments to select operators.



209
210
211
212
213
214
# File 'lib/HDLRuby/hruby_low_bool2select.rb', line 209

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!Object

Extracts the expressions from the casts.



226
227
228
229
230
231
232
# File 'lib/HDLRuby/hruby_low_casts_without_expression.rb', line 226

def casts_without_expression!
    # # Recurse on the sub node.
    # return Unary.new(self.type,self.operator,
    #                  self.child.casts_without_expression)
    self.set_child!(self.child.casts_without_expression!)
    return self
end

#cloneObject

Clones the unary operator (deeply)



5016
5017
5018
# File 'lib/HDLRuby/hruby_low.rb', line 5016

def clone
    return Unary.new(@type,self.operator,@child.clone)
end

#each_deep(&ruby_block) ⇒ Object

Iterates over each object deeply.

Returns an enumerator if no ruby block is given.



4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
# File 'lib/HDLRuby/hruby_low.rb', line 4951

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_node(&ruby_block) ⇒ Object Also known as: each_expression

Iterates over the expression children if any.



4978
4979
4980
4981
4982
4983
# File 'lib/HDLRuby/hruby_low.rb', line 4978

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

#each_node_deep(&ruby_block) ⇒ Object

Iterates over the nodes deeply if any.



4988
4989
4990
4991
4992
4993
4994
4995
# File 'lib/HDLRuby/hruby_low.rb', line 4988

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) ⇒ Object

Iterates over all the references encountered in the expression.

NOTE: do not iterate inside the references.



5000
5001
5002
5003
5004
5005
5006
5007
# File 'lib/HDLRuby/hruby_low.rb', line 5000

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) ⇒ Boolean

Comparison for hash: structural comparison.

Returns:

  • (Boolean)


4963
4964
4965
4966
4967
4968
4969
4970
# File 'lib/HDLRuby/hruby_low.rb', line 4963

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) ⇒ Object

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



268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/HDLRuby/hruby_low_fix_types.rb', line 268

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

#hashObject

Hash function.



4973
4974
4975
# File 'lib/HDLRuby/hruby_low.rb', line 4973

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

#immutable?Boolean

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

Returns:

  • (Boolean)


4943
4944
4945
4946
# File 'lib/HDLRuby/hruby_low.rb', line 4943

def immutable?
    # Immutable if the child is immutable.
    return child.immutable?
end

#to_c(res, level = 0) ⇒ Object

return res end Generates the C text of the equivalent HDLRuby code. +level+ is the hierachical level of the object. def to_c(level = 0)



2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
# File 'lib/HDLRuby/hruby_low2c.rb', line 2247

def to_c(res,level = 0)
    if (self.operator == :+@) then
        # No computation required.
        self.child.to_c(res,level)
        return res
    end
    # Some computation required.
    # Save the value pool state.
    res << (" " * (level*3)) << "PV;\n"
    # Generate the child.
    self.child.to_c(res,level)
    res << (" " * (level*3))
    res << "unary("
    # Adds the operation
    case self.operator
    when :~ then
        res << "&not_value"
    when :-@ then
        res << "&neg_value"
    else
        raise "Invalid unary operator: #{self.operator}."
    end
    res << ");\n"
    # Restore the value pool state.
    res << (" " * (level*3)) << "RV;\n"

    return res
end

#to_hdr(level = 0) ⇒ Object

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



589
590
591
# 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_highObject

Creates a new high unary expression.



416
417
418
419
# 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_verilogObject

Converts the system to Verilog code.



1701
1702
1703
# File 'lib/HDLRuby/hruby_verilog.rb', line 1701

def to_verilog
    return "#{self.operator[0]}#{self.child.to_verilog}"
end

#to_vhdl(level = 0, std_logic = false) ⇒ Object

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.



1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
# File 'lib/HDLRuby/hruby_low2vhd.rb', line 1260

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) ⇒ Boolean

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

Returns:

  • (Boolean)


5010
5011
5012
5013
# File 'lib/HDLRuby/hruby_low.rb', line 5010

def use_name?(*names)
    # Recurse on the child.
    return @child.use_name?(*names)
end