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.
-
#match_type(typ) ⇒ Object
Match the type with +typ+: - Recurse on the sub expr if hierachical type, raising an arror if the expression is not hierarchical.
-
#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.
2427 2428 2429 |
# File 'lib/HDLRuby/hruby_high.rb', line 2427 def systemT @systemT end |
#type ⇒ Object
The type of the expression if any.
2429 2430 2431 |
# File 'lib/HDLRuby/hruby_high.rb', line 2429 def type @type end |
Class Method Details
.orig_operator(op) ⇒ Object
Gets the origin method for operation +op+.
2600 2601 2602 |
# File 'lib/HDLRuby/hruby_high.rb', line 2600 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.
2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 |
# File 'lib/HDLRuby/hruby_high.rb', line 2673 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+.
2527 2528 2529 2530 2531 2532 2533 |
# File 'lib/HDLRuby/hruby_high.rb', line 2527 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.
2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 |
# File 'lib/HDLRuby/hruby_high.rb', line 2643 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.
2505 2506 2507 2508 2509 2510 2511 |
# File 'lib/HDLRuby/hruby_high.rb', line 2505 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.
2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 |
# File 'lib/HDLRuby/hruby_high.rb', line 2472 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.
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 |
# File 'lib/HDLRuby/hruby_high.rb', line 2432 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.
2551 2552 2553 |
# File 'lib/HDLRuby/hruby_high.rb', line 2551 def ljust(n,v) return [(v.to_s * (n-self.width)).to_expr, self] end |
#lr(n) ⇒ Object
Left rotate of +n+ bits.
2631 2632 2633 2634 |
# File 'lib/HDLRuby/hruby_high.rb', line 2631 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.
2621 2622 2623 |
# File 'lib/HDLRuby/hruby_high.rb', line 2621 def ls(n) return self << n end |
#match_type(typ) ⇒ Object
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.
2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 |
# File 'lib/HDLRuby/hruby_high.rb', line 2574 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 |
#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.
2697 2698 2699 2700 2701 2702 2703 |
# File 'lib/HDLRuby/hruby_high.rb', line 2697 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
2603 2604 2605 |
# File 'lib/HDLRuby/hruby_high.rb', line 2603 def orig_operator(op) HExpression.orig_operator(op) end |
#output(name) ⇒ Object
Creates output port +name+ and connect it to the expression.
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 |
# File 'lib/HDLRuby/hruby_high.rb', line 2452 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.
2556 2557 2558 |
# File 'lib/HDLRuby/hruby_high.rb', line 2556 def rjust(n,v) return [self, (v.to_s * (n-self.width)).to_expr] end |
#rr(n) ⇒ Object
Right rotate of +n+ bits.
2637 2638 2639 2640 |
# File 'lib/HDLRuby/hruby_high.rb', line 2637 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.
2626 2627 2628 |
# File 'lib/HDLRuby/hruby_high.rb', line 2626 def rs(n) return self >> n end |
#sext(n) ⇒ Object
Extends on the left to +n+ bits preserving the signe.
2566 2567 2568 |
# File 'lib/HDLRuby/hruby_high.rb', line 2566 def sext(n) return self.ljust(self[-1]) end |
#to_bit ⇒ Object
Casts to a bit vector type.
2536 2537 2538 |
# File 'lib/HDLRuby/hruby_high.rb', line 2536 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.
2517 2518 2519 |
# File 'lib/HDLRuby/hruby_high.rb', line 2517 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.
2541 2542 2543 |
# File 'lib/HDLRuby/hruby_high.rb', line 2541 def to_unsigned return self.as(unsigned[self.width]) end |
#to_value ⇒ Object
Converts to a new value.
NOTE: to be redefined.
2499 2500 2501 2502 |
# File 'lib/HDLRuby/hruby_high.rb', line 2499 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.
2492 2493 2494 |
# File 'lib/HDLRuby/hruby_high.rb', line 2492 def to_value? return false end |
#zext(n) ⇒ Object
Extends on the left to +n+ bits filling with 0.
2561 2562 2563 |
# File 'lib/HDLRuby/hruby_high.rb', line 2561 def zext(n) return self.ljust(n,0) end |