module HDLRuby::High::HExpression
Module giving high-level expression properties
Attributes
The system type the expression has been resolved in, if any.
The type of the expression if resolved.
Public Class Methods
Gets the origin method for operation op
.
# File lib/HDLRuby/hruby_high.rb, line 2570 def self.orig_operator(op) return (op.to_s + "_orig").to_sym end
Public Instance Methods
Creates an access to elements of range rng
of the signal.
NOTE: rng
can be a single expression in which case it is an index.
# File lib/HDLRuby/hruby_high.rb, line 2643 def [](rng) if rng.respond_to?(:to_expr) then # Number range: convert it to an expression. rng = rng.to_expr end if rng.is_a?(HDLRuby::Low::Expression) then # Index case return RefIndex.new(self.type.base,self.to_expr,rng) else # Range case, ensure it is made among expression. first = rng.first.to_expr last = rng.last.to_expr # Abd create the reference. return RefRange.new(self.type.slice(first..last), self.to_expr,first..last) end end
Casts as type
.
# File lib/HDLRuby/hruby_high.rb, line 2501 def as(type) return Cast.new(type.to_type,self.to_expr) end
Coerce by forcing convertion of obj to expression.
# File lib/HDLRuby/hruby_high.rb, line 2613 def coerce(obj) if obj.is_a?(HDLRuby::Low::Expression) then # Already an expression, nothing to do. return [obj,self] elsif obj.respond_to?(:to_expr) then # Can be converted to an expression, do it. return [obj.to_expr, self] else return [obj,self] end end
Tell if the expression is constant.
# File lib/HDLRuby/hruby_high.rb, line 2479 def constant? # By default not constant. return false unless self.each_node.any? # If any sub node, check if all of them are constants. self.each_node { |node| return false unless node.constant? } return true end
Creates inout port name
and connect it to the expression.
# File lib/HDLRuby/hruby_high.rb, line 2446 def inout(name) # Ensures the name is a symbol. name = name.to_sym # Get access to the current expression obj = self # Create the inout. port = nil HDLRuby::High.cur_system.open do port = obj.type.inout(name) end # Make the connection when the instance is ready. HDLRuby::High.cur_system.on_instance do |inst| obj.scope.open do RefObject.new(inst,port.to_ref) <= obj end end return port end
Creates input port name
and connect it to the expression.
# File lib/HDLRuby/hruby_high.rb, line 2406 def input(name) # Ensures the name is a symbol. name = name.to_sym # Get access to the current expression obj = self # Create the input. port = nil HDLRuby::High.cur_system.open do port = obj.type.input(name) end # Make the connection when the instance is ready. HDLRuby::High.cur_system.on_instance do |inst| obj.scope.open do RefObject.new(inst,port.to_ref) <= obj end end return port end
Extends on the left to n
bits filling with v
bit values.
# File lib/HDLRuby/hruby_high.rb, line 2521 def ljust(n,v) return [(v.to_s * (n-self.width)).to_expr, self] end
Left rotate of n
bits.
# File lib/HDLRuby/hruby_high.rb, line 2601 def lr(n) w = self.type.width return [self[w-(n+1)..0], self[w-1..w-(n)]] end
Left shift of n
bits.
# File lib/HDLRuby/hruby_high.rb, line 2591 def ls(n) return self << n end
Match the type with typ
:
-
Recurse on the sub expr if hierachical type, raising an arror if the expression is not hierarchical.
-
Directly cast otherwise.
# File lib/HDLRuby/hruby_high.rb, line 2544 def match_type(typ) # Has the type sub types? if typ.types? then unless self.is_a?(Concat) then raise AnyError, "Invalid class for assignment to hierarchical: #{self.class}." end return Concat.new(typ, self.each_expression.zip(typ.each_type).map do |e,t| e.match_type(t) end) elsif typ.vector? && typ.base.hierarchical? then unless self.is_a?(Concat) then raise AnyError, "Invalid class for assignment to hierarchical: #{self.class}." end return Concat.new(typ, self.each_expression.map do |e| e.match_type(typ.base) end) else return self.as(typ) end end
Converts to a select operator using current expression as condition for one of the choices
.
NOTE: choices
can either be a list of arguments or an array. If
choices
has only two entries (and it is not a hash), value
will be converted to a boolean.
# File lib/HDLRuby/hruby_high.rb, line 2667 def mux(*choices) # Process the choices. choices = choices.flatten(1) if choices.size == 1 choices.map! { |choice| choice.to_expr } # Generate the select expression. return Select.new(choices[0].type,"?",self.to_expr,*choices) end
# File lib/HDLRuby/hruby_high.rb, line 2573 def orig_operator(op) HExpression.orig_operator(op) end
Creates output port name
and connect it to the expression.
# File lib/HDLRuby/hruby_high.rb, line 2426 def output(name) # Ensures the name is a symbol. name = name.to_sym # Get access to the current expression obj = self # Create the output. port = nil HDLRuby::High.cur_system.open do port = obj.type.output(name) end # Make the connection when the instance is ready. HDLRuby::High.cur_system.on_instance do |inst| obj.scope.open do obj <= RefObject.new(inst,port.to_ref) end end return port end
Extends on the right to n
bits filling with v
bit values.
# File lib/HDLRuby/hruby_high.rb, line 2526 def rjust(n,v) return [self, (v.to_s * (n-self.width)).to_expr] end
Right rotate of n
bits.
# File lib/HDLRuby/hruby_high.rb, line 2607 def rr(n) w = self.type.width return [self[(n-1)..0], self[w-1..n]] end
Right shift of n
bits.
# File lib/HDLRuby/hruby_high.rb, line 2596 def rs(n) return self >> n end
Extends on the left to n
bits preserving the signe.
# File lib/HDLRuby/hruby_high.rb, line 2536 def sext(n) return self.ljust(self[-1]) end
Casts to a bit vector type.
# File lib/HDLRuby/hruby_high.rb, line 2506 def to_bit return self.as(bit[self.width]) end
Converts to a new expression.
NOTE: to be redefined in case of non-expression class.
# File lib/HDLRuby/hruby_high.rb, line 2491 def to_expr raise AnyError, "Internal error: to_expr not defined yet for class: #{self.class}" end
Casts to an unsigned bit vector type.
# File lib/HDLRuby/hruby_high.rb, line 2511 def to_unsigned return self.as(unsigned[self.width]) end
Converts to a new value.
NOTE: to be redefined.
# File lib/HDLRuby/hruby_high.rb, line 2473 def to_value raise AnyError, "Expression cannot be converted to a value: #{self.class}" end
Tell if the expression can be converted to a value.
# File lib/HDLRuby/hruby_high.rb, line 2466 def to_value? return false end
Sets the data type
.
# File lib/HDLRuby/hruby_high.rb, line 2684 def type=(type) # Check and set the type. unless type.respond_to?(:htype?) then raise AnyError, "Invalid class for a type: #{type.class}." end @type = type end
Extends on the left to n
bits filling with 0.
# File lib/HDLRuby/hruby_high.rb, line 2531 def zext(n) return self.ljust(n,0) end