Module: HDLRuby::High::HExpression
- Defined in:
- lib/HDLRuby/hruby_high.rb,
lib/HDLRuby/std/sequencer.rb
Overview
Module giving high-level expression properties
Instance Attribute Summary collapse
-
#systemT ⇒ Object
readonly
The system type the expression has been resolved in, if any.
-
#type ⇒ Object
The type of the expression if any.
Class Method Summary collapse
-
.orig_operator(op) ⇒ Object
Gets the origin method for operation +op+.
Instance Method Summary collapse
-
#<=>(expr) ⇒ Object
The <=> operator is also supported by is transformed into a sub with a signed result.
-
#[](rng) ⇒ Object
Creates an access to elements of range +rng+ of the signal.
-
#as(type) ⇒ Object
Casts as +type+.
-
#coerce(obj) ⇒ Object
Coerce by forcing convertion of obj to expression.
-
#constant? ⇒ Boolean
Tell if the expression is constant.
-
#inout(name) ⇒ Object
Creates inout port +name+ and connect it to the expression.
-
#input(name) ⇒ Object
Creates input port +name+ and connect it to the expression.
-
#ljust(n, v) ⇒ Object
Extends on the left to +n+ bits filling with +v+ bit values.
-
#lr(n) ⇒ Object
Left rotate of +n+ bits.
-
#ls(n) ⇒ Object
Left shift of +n+ bits.
-
#match_type(typ) ⇒ Object
Match the type with +typ+: cast if different type.
-
#mux(*choices) ⇒ Object
Converts to a select operator using current expression as condition for one of the +choices+.
- #orig_operator(op) ⇒ Object
-
#output(name) ⇒ Object
Creates output port +name+ and connect it to the expression.
-
#rjust(n, v) ⇒ Object
Extends on the right to +n+ bits filling with +v+ bit values.
-
#rr(n) ⇒ Object
Right rotate of +n+ bits.
-
#rs(n) ⇒ Object
Right shift of +n+ bits.
-
#seach(&ruby_block) ⇒ Object
HW iteration on each element.
-
#sext(n) ⇒ Object
Extends on the left to +n+ bits preserving the signe.
-
#to_bit ⇒ Object
Casts to a bit vector type.
-
#to_expr ⇒ Object
Converts to a new expression.
-
#to_signed ⇒ Object
Casts to a signed bit vector type.
-
#to_unsigned ⇒ Object
Casts to an unsigned bit vector type.
-
#to_value ⇒ Object
Converts to a new value.
-
#to_value? ⇒ Boolean
Tell if the expression can be converted to a value.
-
#zext(n) ⇒ Object
Extends on the left to +n+ bits filling with 0.
Instance Attribute Details
#systemT ⇒ Object (readonly)
The system type the expression has been resolved in, if any.
2748 2749 2750 |
# File 'lib/HDLRuby/hruby_high.rb', line 2748 def systemT @systemT end |
#type ⇒ Object
The type of the expression if any.
2750 2751 2752 |
# File 'lib/HDLRuby/hruby_high.rb', line 2750 def type @type end |
Class Method Details
.orig_operator(op) ⇒ Object
Gets the origin method for operation +op+.
2930 2931 2932 |
# File 'lib/HDLRuby/hruby_high.rb', line 2930 def self.orig_operator(op) return (op.to_s + "_orig").to_sym end |
Instance Method Details
#<=>(expr) ⇒ Object
The <=> operator is also supported by is transformed into a sub with a signed result.
3002 3003 3004 |
# File 'lib/HDLRuby/hruby_high.rb', line 3002 def <=>(expr) return (self.as(self.type.base[self.type.width+1])-expr).to_signed end |
#[](rng) ⇒ Object
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.
3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 |
# File 'lib/HDLRuby/hruby_high.rb', line 3010 def [](rng) if rng.is_a?(::Range) then first = rng.first if (first.is_a?(::Integer)) then first = self.type.size+first if first < 0 end last = rng.last if (last.is_a?(::Integer)) then last = self.type.size+last if last < 0 end rng = first..last end if rng.is_a?(::Integer) && rng < 0 then rng = self.type.size+rng end 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 |
#as(type) ⇒ Object
Casts as +type+.
2848 2849 2850 2851 2852 2853 2854 |
# File 'lib/HDLRuby/hruby_high.rb', line 2848 def as(type) if (self.parent) return Cast.new(type.to_type,self.to_expr) else return Cast.new(type.to_type,self) end end |
#coerce(obj) ⇒ Object
Coerce by forcing convertion of obj to expression.
2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 |
# File 'lib/HDLRuby/hruby_high.rb', line 2973 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 |
#constant? ⇒ Boolean
Tell if the expression is constant.
2826 2827 2828 2829 2830 2831 2832 |
# File 'lib/HDLRuby/hruby_high.rb', line 2826 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 |
#inout(name) ⇒ Object
Creates inout port +name+ and connect it to the expression.
2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 |
# File 'lib/HDLRuby/hruby_high.rb', line 2793 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 |
#input(name) ⇒ Object
Creates input port +name+ and connect it to the expression.
2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 |
# File 'lib/HDLRuby/hruby_high.rb', line 2753 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 |
#ljust(n, v) ⇒ Object
Extends on the left to +n+ bits filling with +v+ bit values.
2872 2873 2874 |
# File 'lib/HDLRuby/hruby_high.rb', line 2872 def ljust(n,v) return [(v.to_s * (n-self.width)).to_expr, self] end |
#lr(n) ⇒ Object
Left rotate of +n+ bits.
2961 2962 2963 2964 |
# File 'lib/HDLRuby/hruby_high.rb', line 2961 def lr(n) w = self.type.width return [self[w-(n+1)..0], self[w-1..w-(n)]] end |
#ls(n) ⇒ Object
Left shift of +n+ bits.
2951 2952 2953 |
# File 'lib/HDLRuby/hruby_high.rb', line 2951 def ls(n) return self << n end |
#match_type(typ) ⇒ Object
Match the type with +typ+: cast if different type.
2921 2922 2923 2924 2925 2926 2927 |
# File 'lib/HDLRuby/hruby_high.rb', line 2921 def match_type(typ) if self.type.eql?(typ) then return self else return self.as(typ) end end |
#mux(*choices) ⇒ Object
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.
3051 3052 3053 3054 3055 3056 3057 |
# File 'lib/HDLRuby/hruby_high.rb', line 3051 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 |
#orig_operator(op) ⇒ Object
2933 2934 2935 |
# File 'lib/HDLRuby/hruby_high.rb', line 2933 def orig_operator(op) HExpression.orig_operator(op) end |
#output(name) ⇒ Object
Creates output port +name+ and connect it to the expression.
2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 |
# File 'lib/HDLRuby/hruby_high.rb', line 2773 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 |
#rjust(n, v) ⇒ Object
Extends on the right to +n+ bits filling with +v+ bit values.
2877 2878 2879 |
# File 'lib/HDLRuby/hruby_high.rb', line 2877 def rjust(n,v) return [self, (v.to_s * (n-self.width)).to_expr] end |
#rr(n) ⇒ Object
Right rotate of +n+ bits.
2967 2968 2969 2970 |
# File 'lib/HDLRuby/hruby_high.rb', line 2967 def rr(n) w = self.type.width return [self[(n-1)..0], self[w-1..n]] end |
#rs(n) ⇒ Object
Right shift of +n+ bits.
2956 2957 2958 |
# File 'lib/HDLRuby/hruby_high.rb', line 2956 def rs(n) return self >> n end |
#seach(&ruby_block) ⇒ Object
HW iteration on each element.
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 |
# File 'lib/HDLRuby/std/sequencer.rb', line 1729 def seach(&ruby_block) # Create the hardware iterator. this = self hw_enum = SEnumeratorBase.new(this.type.base,this.type.size) do |idx| this[idx] end # Is there a ruby block? if(ruby_block) then # Yes, apply it. return hw_enum.seach(&ruby_block) else # No, return the resulting enumerator. return hw_enum end end |
#sext(n) ⇒ Object
Extends on the left to +n+ bits preserving the signe.
2887 2888 2889 |
# File 'lib/HDLRuby/hruby_high.rb', line 2887 def sext(n) return self.ljust(self[-1]) end |
#to_bit ⇒ Object
Casts to a bit vector type.
2857 2858 2859 |
# File 'lib/HDLRuby/hruby_high.rb', line 2857 def to_bit return self.as(HDLRuby::High.top_user.bit[self.type.width]) end |
#to_expr ⇒ Object
Converts to a new expression.
NOTE: to be redefined in case of non-expression class.
2838 2839 2840 |
# File 'lib/HDLRuby/hruby_high.rb', line 2838 def to_expr raise AnyError, "Internal error: to_expr not defined yet for class: #{self.class}" end |
#to_signed ⇒ Object
Casts to a signed bit vector type.
2867 2868 2869 |
# File 'lib/HDLRuby/hruby_high.rb', line 2867 def to_signed return self.as(HDLRuby::High.top_user.signed[self.type.width]) end |
#to_unsigned ⇒ Object
Casts to an unsigned bit vector type.
2862 2863 2864 |
# File 'lib/HDLRuby/hruby_high.rb', line 2862 def to_unsigned return self.as(HDLRuby::High.top_user.unsigned[self.type.width]) end |
#to_value ⇒ Object
Converts to a new value.
NOTE: to be redefined.
2820 2821 2822 2823 |
# File 'lib/HDLRuby/hruby_high.rb', line 2820 def to_value raise AnyError, "Expression cannot be converted to a value: #{self.class}" end |
#to_value? ⇒ Boolean
Tell if the expression can be converted to a value.
2813 2814 2815 |
# File 'lib/HDLRuby/hruby_high.rb', line 2813 def to_value? return false end |
#zext(n) ⇒ Object
Extends on the left to +n+ bits filling with 0.
2882 2883 2884 |
# File 'lib/HDLRuby/hruby_high.rb', line 2882 def zext(n) return self.ljust(n,0) end |