Module: HDLRuby::Tprocess
- Included in:
- High::Htype
- Defined in:
- lib/HDLRuby/hruby_types.rb
Overview
To include to classes for type processing support.
Instance Method Summary collapse
-
#&(type) ⇒ Object
(also: #|, #^, #<, #>, #<=, #>=, #<=>)
And.
-
#*(type) ⇒ Object
Multiplication.
-
#+(type) ⇒ Object
(also: #-)
Addition.
-
#+@ ⇒ Object
Positive.
-
#-@ ⇒ Object
Negative.
-
#/(type) ⇒ Object
(also: #%)
Division.
-
#<<(type) ⇒ Object
(also: #ls, #>>, #rs)
Shift left.
-
#==(type) ⇒ Object
(also: #!=)
Equals alias_method :==, :&.
-
#abs ⇒ Object
Absolute value.
-
#lr(type) ⇒ Object
(also: #rr)
Rotate left.
-
#make(name, base, range) ⇒ Object
Creates a new generic vector type named +name+ from +base+ type and with +range+.
-
#resolve(type) ⇒ Object
Type resolution: decide which class to use for representing a computating result with +type+.
-
#slice(idx) ⇒ Object
Range access with +idx+ NOTE: - +idx+ may be a range.
-
#~ ⇒ Object
Not.
Instance Method Details
#&(type) ⇒ Object Also known as: |, ^, <, >, <=, >=, <=>
And
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/HDLRuby/hruby_types.rb', line 153 def &(type) # puts "compute types with=#{self} and #{type}" # Resolve the type class. resolved = self.resolve(type) # Logical operation on non-vector types are kept as is. return resolved unless resolved.is_a?(TypeVector) # Otherwise the range is computed. # New type range: largest range bounds = [ self.range.first.to_i, type.range.first.to_i, self.range.last.to_i, type.range.last.to_i ] # puts "bounds=#{bounds}" res_lsb = bounds.min res_msb = bounds.max # Create and return the new type: its endianess is the one of self if self.range.first.to_i > self.range.last.to_i then return resolved.make(:"",resolved.base,res_msb..res_lsb) else return resolved.make(:"",resolved.base,res_lsb..res_msb) end end |
#*(type) ⇒ Object
Multiplication
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/HDLRuby/hruby_types.rb', line 98 def *(type) # Resolve the type class. resolved = self.resolve(type) # New type range: largest range * 2 bounds = [ self.range.first.to_i, type.range.first.to_i, self.range.last.to_i, type.range.last.to_i ] res_lsb = bounds.min res_msb = bounds.max * 2 # Create and return the new type: its endianess is the one of self if self.range.first.to_i > self.range.last.to_i then return resolved.make(:"",resolved.base,res_msb..res_lsb) else return resolved.make(:"",resolved.base,res_lsb..res_msb) end end |
#+(type) ⇒ Object Also known as: -
Addition.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/HDLRuby/hruby_types.rb', line 78 def +(type) # Resolve the type class. resolved = self.resolve(type) # New type range: largest range + 1 bounds = [ self.range.first.to_i, type.range.first.to_i, self.range.last.to_i, type.range.last.to_i ] res_lsb = bounds.min res_msb = bounds.max + 1 # Create and return the new type: its endianess is the one of self if self.range.first.to_i > self.range.last.to_i then return resolved.make(:"",resolved.base,res_msb..res_lsb) else return resolved.make(:"",resolved.base,res_lsb..res_msb) end end |
#+@ ⇒ Object
Positive
135 136 137 |
# File 'lib/HDLRuby/hruby_types.rb', line 135 def +@() return self end |
#-@ ⇒ Object
Negative
140 141 142 |
# File 'lib/HDLRuby/hruby_types.rb', line 140 def -@() return self end |
#/(type) ⇒ Object Also known as: %
Division
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/HDLRuby/hruby_types.rb', line 115 def /(type) # Resolve the type class. resolved = self.resolve(type) # New type range: largest range bounds = [ self.range.first.to_i, type.range.first.to_i, self.range.last.to_i, type.range.last.to_i ] res_lsb = bounds.min res_msb = bounds.max # Create and return the new type: its endianess is the one of self if self.range.first.to_i > self.range.last.to_i then return resolved.make(:"",resolved.base,res_msb..res_lsb) else return resolved.make(:"",resolved.base,res_lsb..res_msb) end end |
#<<(type) ⇒ Object Also known as: ls, >>, rs
Shift left
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/HDLRuby/hruby_types.rb', line 213 def <<(type) # The result type is the type of left. resolved = self # New type range: 2**(type width) times self range bounds = [ self.range.first.to_i, self.range.last.to_i ] res_lsb = bounds.min res_msb = bounds.max + (2 ** ((type.range.last-type.range.first).abs)) # Create and return the new type: its endianess is the one of self if self.range.first.to_i > self.range.last.to_i then return resolved.make(:"",resolved.base,res_msb..res_lsb) else return resolved.make(:"",resolved.base,res_lsb..res_msb) end end |
#==(type) ⇒ Object Also known as: !=
Equals alias_method :==, :&
189 190 191 |
# File 'lib/HDLRuby/hruby_types.rb', line 189 def ==(type) return Bit end |
#abs ⇒ Object
Absolute value
145 146 147 |
# File 'lib/HDLRuby/hruby_types.rb', line 145 def abs() return self end |
#lr(type) ⇒ Object Also known as: rr
Rotate left.
236 237 238 |
# File 'lib/HDLRuby/hruby_types.rb', line 236 def lr(type) return self end |
#make(name, base, range) ⇒ Object
Creates a new generic vector type named +name+ from +base+ type and with +range+. NOTE: used for type processing.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/HDLRuby/hruby_types.rb', line 15 def make(name,base,range) # Generate a vector or a scalar type depending on the range. # First for checking the rangem ensures the bounds are Ruby # values. first = range.first last = range.last first = first.content if first.is_a?(Value) last = last.content if last.is_a?(Value) # Necessarily a TypeVector, since [0..0] has actually a # different meaning from [0]! # # Now can compare at Ruby level (and not HDLRuby level). # if first == last then # # Single-element, return the base. # return base # else # # Multiple elements, create a new type vector. # return TypeVector.new(name,base,range) # end return TypeVector.new(name,base,range) end |
#resolve(type) ⇒ Object
Type resolution: decide which class to use for representing a computating result with +type+.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/HDLRuby/hruby_types.rb', line 38 def resolve(type) # puts "self=#{self} type=#{type}" if self.float? then return self elsif type.float? then return type elsif self.signed? then return self elsif type.signed? then return type elsif self.unsigned? then return self elsif type.unsigned? then return type elsif self.width >= type.width then return self else return type end end |
#slice(idx) ⇒ Object
Range access with +idx+ NOTE:
- +idx+ may be a range.
- Do not use the [] operator for this since it is used for defining vector types!
64 65 66 67 68 69 70 71 72 |
# File 'lib/HDLRuby/hruby_types.rb', line 64 def slice(idx) if idx.is_a?(Range) then # Make a resized vector. return make(:"",self.base,idx) else # Return the base type. return self.base end end |
#~ ⇒ Object
Not
183 184 185 |
# File 'lib/HDLRuby/hruby_types.rb', line 183 def ~() return self end |