Module: HDLRuby::High::HExpression
- Defined in:
- lib/HDLRuby/hruby_high.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
-
#[](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.
-
#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.
-
#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_unsigned ⇒ Object
Casts to a signed 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.
2262 2263 2264 |
# File 'lib/HDLRuby/hruby_high.rb', line 2262 def systemT @systemT end |
#type ⇒ Object
The type of the expression if any.
2264 2265 2266 |
# File 'lib/HDLRuby/hruby_high.rb', line 2264 def type @type end |
Class Method Details
.orig_operator(op) ⇒ Object
Gets the origin method for operation +op+.
2402 2403 2404 |
# File 'lib/HDLRuby/hruby_high.rb', line 2402 def self.orig_operator(op) return (op.to_s + "_orig").to_sym end |
Instance Method Details
#[](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.
2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 |
# File 'lib/HDLRuby/hruby_high.rb', line 2475 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 |
#as(type) ⇒ Object
Casts as +type+.
2362 2363 2364 |
# File 'lib/HDLRuby/hruby_high.rb', line 2362 def as(type) return Cast.new(type.to_type,self.to_expr) end |
#coerce(obj) ⇒ Object
Coerce by forcing convertion of obj to expression.
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 |
# File 'lib/HDLRuby/hruby_high.rb', line 2445 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.
2340 2341 2342 2343 2344 2345 2346 |
# File 'lib/HDLRuby/hruby_high.rb', line 2340 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.
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 |
# File 'lib/HDLRuby/hruby_high.rb', line 2307 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.
2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 |
# File 'lib/HDLRuby/hruby_high.rb', line 2267 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.
2382 2383 2384 |
# File 'lib/HDLRuby/hruby_high.rb', line 2382 def ljust(n,v) return [(v.to_s * (n-self.width)).to_expr, self] end |
#lr(n) ⇒ Object
Left rotate of +n+ bits.
2433 2434 2435 2436 |
# File 'lib/HDLRuby/hruby_high.rb', line 2433 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.
2423 2424 2425 |
# File 'lib/HDLRuby/hruby_high.rb', line 2423 def ls(n) return self << n 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.
2500 2501 2502 2503 2504 2505 2506 |
# File 'lib/HDLRuby/hruby_high.rb', line 2500 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
2405 2406 2407 |
# File 'lib/HDLRuby/hruby_high.rb', line 2405 def orig_operator(op) HExpression.orig_operator(op) end |
#output(name) ⇒ Object
Creates output port +name+ and connect it to the expression.
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 |
# File 'lib/HDLRuby/hruby_high.rb', line 2287 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.
2387 2388 2389 |
# File 'lib/HDLRuby/hruby_high.rb', line 2387 def rjust(n,v) return [self, (v.to_s * (n-self.width)).to_expr] end |
#rr(n) ⇒ Object
Right rotate of +n+ bits.
2439 2440 2441 2442 |
# File 'lib/HDLRuby/hruby_high.rb', line 2439 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.
2428 2429 2430 |
# File 'lib/HDLRuby/hruby_high.rb', line 2428 def rs(n) return self >> n end |
#sext(n) ⇒ Object
Extends on the left to +n+ bits preserving the signe.
2397 2398 2399 |
# File 'lib/HDLRuby/hruby_high.rb', line 2397 def sext(n) return self.ljust(self[-1]) end |
#to_bit ⇒ Object
Casts to a bit vector type.
2367 2368 2369 |
# File 'lib/HDLRuby/hruby_high.rb', line 2367 def to_bit return self.as(bit[self.width]) end |
#to_expr ⇒ Object
Converts to a new expression.
NOTE: to be redefined in case of non-expression class.
2352 2353 2354 |
# File 'lib/HDLRuby/hruby_high.rb', line 2352 def to_expr raise AnyError, "Internal error: to_expr not defined yet for class: #{self.class}" end |
#to_unsigned ⇒ Object
Casts to a signed bit vector type.
2372 2373 2374 |
# File 'lib/HDLRuby/hruby_high.rb', line 2372 def to_unsigned return self.as(unsigned[self.width]) end |
#to_value ⇒ Object
Converts to a new value.
NOTE: to be redefined.
2334 2335 2336 2337 |
# File 'lib/HDLRuby/hruby_high.rb', line 2334 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.
2327 2328 2329 |
# File 'lib/HDLRuby/hruby_high.rb', line 2327 def to_value? return false end |
#zext(n) ⇒ Object
Extends on the left to +n+ bits filling with 0.
2392 2393 2394 |
# File 'lib/HDLRuby/hruby_high.rb', line 2392 def zext(n) return self.ljust(n,0) end |