Class: HDLRuby::Low::If

Inherits:
Statement show all
Defined in:
lib/HDLRuby/hruby_low.rb,
lib/HDLRuby/hruby_viz.rb,
lib/HDLRuby/hruby_low2c.rb,
lib/HDLRuby/hruby_low2hdr.rb,
lib/HDLRuby/hruby_low2seq.rb,
lib/HDLRuby/hruby_low2vhd.rb,
lib/HDLRuby/hruby_verilog.rb,
lib/HDLRuby/hruby_low2high.rb,
lib/HDLRuby/hruby_low_cleanup.rb,
lib/HDLRuby/hruby_low_mutable.rb,
lib/HDLRuby/hruby_low_skeleton.rb,
lib/HDLRuby/hruby_low_with_var.rb,
lib/HDLRuby/hruby_low_fix_types.rb,
lib/HDLRuby/hruby_low_bool2select.rb,
lib/HDLRuby/hruby_low_without_select.rb,
lib/HDLRuby/hruby_low_without_parinseq.rb,
lib/HDLRuby/hruby_low_without_namespace.rb,
lib/HDLRuby/hruby_low_without_subsignals.rb,
lib/HDLRuby/hruby_low_casts_without_expression.rb

Overview

Describes an if statement.

Direct Known Subclasses

High::If

Constant Summary

Constants included from Low2Symbol

Low2Symbol::Low2SymbolPrefix, Low2Symbol::Low2SymbolTable, Low2Symbol::Symbol2LowTable

Instance Attribute Summary collapse

Attributes included from Hparent

#parent

Instance Method Summary collapse

Methods inherited from Statement

#add_blocks_code, #add_make_block, #behavior, #block, #break_types!, #parent_system, #scope, #top_block, #top_scope, #with_boolean!

Methods included from Low2Symbol

#to_sym

Methods included from Hparent

#absolute_ref, #hierarchy, #no_parent!, #scope

Constructor Details

#initialize(condition, yes, no = nil) ⇒ If

Creates a new if statement with a +condition+ and a +yes+ and +no+ blocks.



3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
# File 'lib/HDLRuby/hruby_low.rb', line 3496

def initialize(condition, yes, no = nil)
    # Check and set the condition.
    unless condition.is_a?(Expression)
        raise AnyError,
              "Invalid class for a condition: #{condition.class}"
    end
    super()
    @condition = condition
    # And set its parent.
    condition.parent = self
    # Check and set the yes statement.
    unless yes.is_a?(Statement)
        raise AnyError, "Invalid class for a statement: #{yes.class}"
    end
    @yes = yes
    # And set its parent.
    yes.parent = self
    # Check and set the yes statement.
    if no and !no.is_a?(Statement)
        raise AnyError, "Invalid class for a statement: #{no.class}"
    end
    @no = no
    # And set its parent.
    no.parent = self if no

    # Initialize the list of alternative if statements (elsif)
    @noifs = []
end

Instance Attribute Details

#conditionObject (readonly)

The condition



3489
3490
3491
# File 'lib/HDLRuby/hruby_low.rb', line 3489

def condition
  @condition
end

#noObject

The yes and no statements



3492
3493
3494
# File 'lib/HDLRuby/hruby_low.rb', line 3492

def no
  @no
end

#yesObject (readonly)

The yes and no statements



3492
3493
3494
# File 'lib/HDLRuby/hruby_low.rb', line 3492

def yes
  @yes
end

Instance Method Details

#add_noif(next_cond, next_yes) ⇒ Object

Adds an alternative if statement (elsif) testing +next_cond+ and executing +next_yes+ when the condition is met.



3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
# File 'lib/HDLRuby/hruby_low.rb', line 3579

def add_noif(next_cond, next_yes)
    # Check the condition.
    unless next_cond.is_a?(Expression)
        raise AnyError, 
              "Invalid class for a condition: #{next_cond.class}"
    end
    # And set its parent.
    next_cond.parent = self
    # Check yes statement.
    unless next_yes.is_a?(Statement)
        raise AnyError, 
              "Invalid class for a statement: #{next_yes.class}"
    end
    # And set its parent.
    next_yes.parent = self
    # Add the statement.
    @noifs << [next_cond,next_yes]
end

#blocks2seq!Object

Converts the par sub blocks to seq.



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/HDLRuby/hruby_low2seq.rb', line 104

def blocks2seq!
    # Convert each sub block.
    # If block.
    self.yes.blocks2seq!
    # Elsif blocks
    self.each_noif do |cond, stmnt|
        stmnt.blocks2seq!
    end
    # Else block if any.
    self.no.blocks2seq! if self.no
    return self
end

#boolean_in_assign2select!Object

Converts booleans in assignments to select operators.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/HDLRuby/hruby_low_bool2select.rb', line 96

def boolean_in_assign2select!
    # No need to apply on condition!
    # # Apply on the condition.
    # self.set_condition!(self.condition.boolean_in_assign2select)
    # Apply on the yes.
    self.yes.boolean_in_assign2select!
    # Apply on the noifs.
    @noifs.map! do |cond,stmnt|
        # No need to apply on condition!
        # [cond.boolean_in_assign2select,stmnt.boolean_in_assign2select!]
        [cond,stmnt.boolean_in_assign2select!]
    end
    # Apply on the no if any.
    self.no.boolean_in_assign2select! if self.no
    return self
end

#casts_without_expression!Object

Extracts the expressions from the casts.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/HDLRuby/hruby_low_casts_without_expression.rb', line 85

def casts_without_expression!
    # Apply on the condition.
    self.set_condition!(self.condition.casts_without_expression!)
    # Apply on the yes.
    self.yes.casts_without_expression!
    # Apply on the noifs.
    @noifs.map! do |cond,stmnt|
        [cond.casts_without_expression!,stmnt.casts_without_expression!]
    end
    # Apply on the no if any.
    self.no.casts_without_expression! if self.no
    return self
end

#cloneObject

Clones the If (deeply)



3706
3707
3708
3709
3710
3711
3712
3713
3714
# File 'lib/HDLRuby/hruby_low.rb', line 3706

def clone
    # Duplicate the if.
    res = If.new(@condition.clone, @yes.clone, @no ? @no.clone : nil)
    # Duplicate the alternate ifs
    @noifs.each do |next_cond,next_yes|
        res.add_noif(next_cond.clone,next_yes.clone)
    end
    return res
end

#delete_noif!(noif) ⇒ Object

Deletes an alternate if.



713
714
715
716
717
718
719
720
721
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 713

def delete_noif!(noif)
    if @noifs.include?(noif) then
        # The noif is present, delete it.
        @noifs.delete(noif)
        # And remove its parent.
        noif.parent = nil
    end
    noif
end

#delete_related!(*names) ⇒ Object

Deletes the elements related to one of +names+: either they have one of the names or they use an element with these names. NOTE: only delete actual instantiated elements, types or systemTs are left as is.



782
783
784
785
786
787
788
789
790
791
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 782

def delete_related!(*names)
    # Delete the noifs if their condition uses one of names.
    @noifs.delete_if { |noif| noif[0].use_names?(names) }
    # Recurse on the yes.
    @yes.delete_related!(*names)
    # Recurse on the no.
    @no.delete_related!(*names)
    # Recruse one the no ifs statements.
    @noifs.each { |noif| noif[1].delete_related!(*names) }
end

#delete_unless!(keep) ⇒ Object

Removes the signals and corresponding assignments whose name is not in +keep+.



127
128
129
130
131
132
133
134
135
# File 'lib/HDLRuby/hruby_low_cleanup.rb', line 127

def delete_unless!(keep)
    # Recurse on the sub statements.
    # Yes.
    self.yes.delete_unless!(keep)
    # Noifs.
    self.each_noif { |cond,stmnt| stmnt.delete_unless!(keep) }
    # No if any.
    self.no.delete_unless!(keep) if self.no
end

#each_block(&ruby_block) ⇒ Object

Iterates over the sub blocks.



3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
# File 'lib/HDLRuby/hruby_low.rb', line 3658

def each_block(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_block) unless ruby_block
    # A ruby block?
    # Apply it on the yes, the alternate ifs and the no blocks.
    ruby_block.call(@yes) if @yes.is_a?(Block)
    @noifs.each do |next_cond,next_yes|
        ruby_block.call(next_yes) if next_yes.is_a?(Block)
    end
    ruby_block.call(@no) if @no.is_a?(Block)
end

#each_block_deep(&ruby_block) ⇒ Object

Iterates over all the blocks contained in the current block.



3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
# File 'lib/HDLRuby/hruby_low.rb', line 3686

def each_block_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_block_deep) unless ruby_block
    # A ruby block?
    # Apply it on the yes, the alternate ifs and the no blocks.
    @yes.each_block_deep(&ruby_block)
    @noifs.each do |next_cond,next_yes|
        next_yes.each_block_deep(&ruby_block)
    end
    # @no.each_block_deep(&ruby_block) if @no.is_a?(Block)
    @no.each_block_deep(&ruby_block) if @no
end

#each_deep(&ruby_block) ⇒ Object

Iterates over each object deeply.

Returns an enumerator if no ruby block is given.



3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
# File 'lib/HDLRuby/hruby_low.rb', line 3528

def each_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_deep) unless ruby_block
    # A ruby block? First apply it to current.
    ruby_block.call(self)
    # Then apply on the condition.
    self.condition.each_deep(&ruby_block)
    # Then apply on the yes.
    self.yes.each_deep(&ruby_block)
    # The apply on the no.
    self.no.each_deep(&ruby_block)
    # Then apply on the alternate ifs.
    self.each_noif do |cond,stmnt|
        cond.each_deep(&ruby_block)
        stmnt.each_deep(&ruby_block)
    end
end

#each_node(&ruby_block) ⇒ Object

Iterates over the children (including the condition).

Returns an enumerator if no ruby block is given.



3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
# File 'lib/HDLRuby/hruby_low.rb', line 3627

def each_node(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_node) unless ruby_block
    # A ruby block?
    # Appy it on the children.
    ruby_block.call(@condition)
    ruby_block.call(@yes)
    self.each_noif do |next_cond,next_yes|
        ruby_block.call(next_cond)
        ruby_block.call(next_yes)
    end
    ruby_block.call(@no) if @no
end

#each_node_deep(&ruby_block) ⇒ Object

Iterates over the nodes deeply if any.



3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
# File 'lib/HDLRuby/hruby_low.rb', line 3642

def each_node_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_node_deep) unless ruby_block
    # A ruby block? First apply it to current.
    ruby_block.call(self)
    # And recurse on the children
    @condition.each_node_deep(&ruby_block)
    @yes.each_node_deep(&ruby_block)
    self.each_noif do |next_cond,next_yes|
        next_cond.each_node_deep(&ruby_block)
        next_yes.each_node_deep(&ruby_block)
    end
    @no.each_node_deep(&ruby_block) if @no
end

#each_noif(&ruby_block) ⇒ Object

Iterates over the alternate if statements (elsif).



3599
3600
3601
3602
3603
3604
3605
3606
3607
# File 'lib/HDLRuby/hruby_low.rb', line 3599

def each_noif(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_noif) unless ruby_block
    # A ruby block?
    # Appy it on the alternate if statements.
    @noifs.each do |next_cond,next_yes|
        yield(next_cond,next_yes)
    end
end

#each_statement(&ruby_block) ⇒ Object

Iterates over each sub statement if any.

Returns an enumerator if no ruby block is given.



3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
# File 'lib/HDLRuby/hruby_low.rb', line 3612

def each_statement(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_statement) unless ruby_block
    # A ruby block?
    # Appy it on the statement children.
    ruby_block.call(@yes)
    self.each_noif do |next_cond,next_yes|
        ruby_block.call(next_yes)
    end
    ruby_block.call(@no) if @no
end

#each_statement_deep(&ruby_block) ⇒ Object

Iterates over all the stamements of the block and its sub blocks.



3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
# File 'lib/HDLRuby/hruby_low.rb', line 3671

def each_statement_deep(&ruby_block)
    # No ruby statement? Return an enumerator.
    return to_enum(:each_statement_deep) unless ruby_block
    # A ruby block?
    # Apply it on self.
    ruby_block.call(self)
    # And recurse on the alternate ifs and the no statements.
    @yes.each_statement_deep(&ruby_block)
    @noifs.each do |next_cond,next_yes|
        next_yes.each_statement_deep(&ruby_block)
    end
    @no.each_statement_deep(&ruby_block) if @no.is_a?(Block)
end

#eql?(obj) ⇒ Boolean

Comparison for hash: structural comparison.

Returns:

  • (Boolean)


3547
3548
3549
3550
3551
3552
3553
# File 'lib/HDLRuby/hruby_low.rb', line 3547

def eql?(obj)
    return false unless obj.is_a?(If)
    return false unless @condition.eql?(obj.condition)
    return false unless @yes.eql?(obj.yes)
    return false unless @no.eql?(obj.no)
    return true
end

#explicit_types!Object

Explicit the types conversions in the if.



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/HDLRuby/hruby_low_fix_types.rb', line 139

def explicit_types!
    # Recurse on the condition: it must be a Bit.
    self.set_condition!(self.condition.explicit_types(Bit))
    # Recurse on the yes block.
    self.yes.explicit_types!
    # Recruse on the alternative ifs, the conditions must be Bit.
    self.map_noifs! do |cond,block|
        [ cond.explicit_types(Bit), block.explicit_types! ]
    end
    # Recurse on the no block.
    self.no.explicit_types! if self.no
    return self
end

#extract_declares!Object

Extract the declares from the scope and returns them into an array.

NOTE: do not recurse into the sub scopes or behaviors!



606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 606

def extract_declares!
    # The extracted declares.
    decls = []
    # Recurse on the sub blocks.
    # Yes.
    decls << self.yes.extract_declares!
    # Noifs.
    decls << self.each_noif.map do |cond,stmnt|
        stmnt.extract_declares!
    end
    # No if any.
    decls << self.no.extract_declares! if self.no
    # Returns the extracted declares.
    return decls
end

#extract_selects!Object

Extract the Select expressions.

NOTE: work on the condition only.



206
207
208
209
210
# File 'lib/HDLRuby/hruby_low_without_select.rb', line 206

def extract_selects!
    selects = []
    self.set_condition!(self.condition.extract_selects_to!(selects))
    return selects
end

#fix_scope_refnames!(scopes) ⇒ Object

Fix the references names using scopes given in +scopes + list (they are marked to be deleted).



639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 639

def fix_scope_refnames!(scopes)
    # Fix the condition.
    self.set_condition!(self.condition.fix_scope_refnames!(scopes))
    # Recurse on the yes.
    self.yes.fix_scope_refnames!(scopes)
    # Recurse on the alternate ifs.
    self.map_noifs! do |cond,stmnt|
        cond = cond.fix_scope_refnames!(scopes)
        stmnt = stmnt.fix_scope_refnames!(scopes)
        [cond,stmnt]
    end
    # Recruse on the no if any.
    self.no.fix_scope_refnames!(scopes) if self.no
    return self
end

#hashObject

Hash function.



3556
3557
3558
# File 'lib/HDLRuby/hruby_low.rb', line 3556

def hash
    return [@condition,@yes,@no].hash
end

#map_nodes!(&ruby_block) ⇒ Object

Maps on the children (including the condition).



735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 735

def map_nodes!(&ruby_block)
    @condition = ruby_block.call(@condition)
    @yes = ruby_block.call(@yes)
    self.map_noifs! do |cond,stmnt|
        [ruby_block.call(cond), ruby_block.call(stmnt)]
    end
    # @noifs.map! do |cond,stmnt|
    #     cond  = ruby_block.call(cond)
    #     stmnt = ruby_block.call(stmnt)
    #     cond.parent  = self unless cond.parent
    #     stmnt.parent = self unless stmnt.parent
    #     [cond,stmnt]
    # end
    @no = ruby_block.call(@no) if @no
end

#map_noifs!(&ruby_block) ⇒ Object

Maps on the noifs.



724
725
726
727
728
729
730
731
732
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 724

def map_noifs!(&ruby_block)
    @noifs.map! do |cond,stmnt|
        cond,stmnt  = ruby_block.call(cond,stmnt)
        # cond, stmnt  = ruby_block.call(cond), ruby_block.call(stmnt)
        cond.parent  = self unless cond.parent
        stmnt.parent = self unless stmnt.parent
        [cond,stmnt]
    end
end

#mix?(mode = nil) ⇒ Boolean

Tell if there is a mix block. +mode+ is the mode of the upper block.

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
128
129
# File 'lib/HDLRuby/hruby_low2seq.rb', line 119

def mix?(mode = nil)
    # Check each sub block.
    # If block.
    return true if self.yes.mix?(mode)
    # Elsif blocks
    self.each_noif do |cond, stmnt|
        return true if stmnt.mix?(mode)
    end
    # Else block if any.
    true if self.no && self.no.mix?(mode)
end

#par_in_seq2seq!Object

Converts par blocks within seq blocks to seq blocks.



66
67
68
69
70
71
72
# File 'lib/HDLRuby/hruby_low_without_parinseq.rb', line 66

def par_in_seq2seq!
    self.yes.par_in_seq2seq!
    self.each_noif do |cond,blk|
        blk.par_in_seq2seq!
    end
    self.no.par_in_seq2seq! if self.no
end

#replace_expressions!(node2rep) ⇒ Object

Replaces sub expressions using +node2rep+ table indicating the node to replace and the corresponding replacement. Returns the actually replaced nodes and their corresponding replacement.

NOTE: the replacement is duplicated.



757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 757

def replace_expressions!(node2rep)
    # First recurse on the children.
    res = {}
    self.each_node do |node|
        res.merge!(node.replace_expressions!(node2rep))
    end
    # Is there a replacement to do on the condition?
    rep = node2rep[self.condition]
    if rep then
        # Yes, do it.
        rep = rep.clone
        node = self.condition
        # node.set_parent!(nil)
        self.set_condition!(rep)
        # And register the replacement.
        res[node] = rep
    end

    return res
end

#replace_names!(former, nname) ⇒ Object

Replaces recursively +former+ name by +nname+ until it is redeclared.



623
624
625
626
627
628
629
630
631
632
633
634
635
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 623

def replace_names!(former,nname)
    # Recurse on the condition.
    self.condition.replace_names!(former,nname)
    # Recurse on the yes.
    self.yes.replace_names!(former,nname)
    # Recurse on the alternate ifs.
    self.each_noif do |cond,stmnt| 
        cond.replace_names!(former,nname)
        stmnt.replace_names!(former,nname)
    end
    # Recurse on the no if any.
    self.no.replace_names!(former,nname) if self.no
end

#set_condition!(condition) ⇒ Object

Sets the condition.



679
680
681
682
683
684
685
686
687
688
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 679

def set_condition!(condition)
    # Check and set the condition.
    unless condition.is_a?(Expression)
        raise AnyError,
              "Invalid class for a condition: #{condition.class}"
    end
    @condition = condition
    # And set its parent.
    condition.parent = self
end

#set_no!(no) ⇒ Object

Sets the no block.



702
703
704
705
706
707
708
709
710
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 702

def set_no!(no)
    # Check and set the yes statement.
    if no and !no.is_a?(Statement)
        raise AnyError, "Invalid class for a statement: #{no.class}"
    end
    @no = no
    # And set its parent.
    no.parent = self if no
end

#set_yes!(yes) ⇒ Object

Sets the yes block.



691
692
693
694
695
696
697
698
699
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 691

def set_yes!(yes)
    # Check and set the yes statement.
    unless yes.is_a?(Statement)
        raise AnyError, "Invalid class for a statement: #{yes.class}"
    end
    @yes = yes
    # And set its parent.
    yes.parent = self
end

#signal2subs!Object

Decompose the hierarchical signals in the statements.



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/HDLRuby/hruby_low_without_subsignals.rb', line 176

def signal2subs!
    # Recurse on the condition.
    self.set_condition!(self.condition.signal2subs!)
    # Recurse on the yes block.
    self.yes.signal2subs!
    # Recurse on the no block if any.
    self.no.signal2subs! if self.no
    # Recurse on the alternate ifs.
    self.map_noifs! do |cond,stmnt|
        [cond.signal2subs!,stmnt.signal2subs!]
    end
    return self
end

#to_c(res, level = 0) ⇒ Object

Generates the C text of the equivalent HDLRuby code.

+level+ is the hierachical level of the object.

def to_c(level = 0)

def to_c(res,level = 0) # The result string. # res = " " * level*3 res << " " * level*3 # Compute the condition. res << " res << " " * (level+1)*3 # res << "Value cond = " << self.condition.to_c(level+1) << ";\n" res << "Value cond = " self.condition.to_c(res,level+1) res << ";\n" # Ensure the condition is testable. res << " " * (level+1)*3 res << "if (is_defined_value(cond)) {\n" # The condition is testable. res << " " * (level+2)*3 res << "if (value2integer(cond)) {\n" # Generate the yes part. # res << self.yes.to_c(level+3) self.yes.to_c(res,level+3) res << " " * level*3 res << "\n" # Generate the alternate if parts. self.each_noif do |cond,stmnt| res << " " * level*3 # res << "else if (value2integer(" << cond.to_c(level+1) << ")) res << "else if (value2integer(" cond.to_c(res,level+1) res << ")) {\n" # res << stmnt.to_c(level+1) stmnt.to_c(res,level+1) res << " " * level*3 res << "\n" end # Generate the no part if any. if self.no then res << " " * level*3 # res << "else << self.no.to_c(level+1) res << "else {\n" self.no.to_c(res,level+1) res << " " * level*3 res << "\n" end # Close the if. res << " " * (level+1)*3 res << "}\n" res << " " * (level)*3 res << "}\n" # Return the result. return res end Generates the C text of the equivalent HDLRuby code. +level+ is the hierachical level of the object.



1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
# File 'lib/HDLRuby/hruby_low2c.rb', line 1625

def to_c(res,level = 0)
    res << " " * level*3
    # Compute the condition.
    self.condition.to_c(res,level)
    # Check is the value is true.
    res << " " * level*3
    res << "if (is_true()) {\n"
    # Generate the yes part.
    self.yes.to_c(res,level+1)
    res << " " * level*3
    res << "}\n"
    # Generate the alternate if parts.
    self.each_noif do |cond,stmnt|
        res << " " * (level*3)
        res << "else {\n"
        cond.to_c(res,level+1)
        # Check is the value is true.
        res << " " * (level+1)*3
        res << "if (is_true()) {\n"
        stmnt.to_c(res,level+2)
        res << " " * ((level+1)*3)
        res << "}\n"
    end
    # Generate the no part if any.
    if self.no then
        res << " " * (level*3)
        res << "else {\n"
        self.no.to_c(res,level+1)
        res << " " * level*3
        res << "}\n"
    end
    # Close the noifs.
    self.each_noif do |cond,stmnt|
        res << " " * (level*3)
        res << "}\n"
    end
    # # Close the if.
    # res << " " * ((level+1)*3)
    # res << "}\n"
    # res << " " * (level*3)
    # res << "}\n"
    # Return the result.
    return res
end

#to_ch(res) ⇒ Object

Generates the content of the h file. def to_ch



1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
# File 'lib/HDLRuby/hruby_low2c.rb', line 1672

def to_ch(res)
    # res = ""
    # Recurse on the sub statements.
    # res << self.yes.to_ch
    self.yes.to_ch(res)
    self.each_noif do |cond,stmnt|
        # res << stmnt.to_ch
        stmnt.to_ch(res)
    end
    # res << self.no.to_ch if self.no
    self.no.to_ch(res) if self.no
    return res
end

#to_hdr(level = 0) ⇒ Object

Generates the text of the equivalent hdr text. +level+ is the hierachical level of the object.



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/HDLRuby/hruby_low2hdr.rb', line 357

def to_hdr(level = 0)
    # The result string.
    res = " " * (level*3)
    # Generate the test.
    res << "hif " << self.condition.to_hdr(level) << " do\n"
    # Generate the yes part.
    res << self.yes.to_hdr(level+1)
    res << " " * (level*3) << "end\n"
    # Generate the alternate if parts.
    self.each_noif do |cond,stmnt|
        res << " " * (level*3)
        res << "helsif " << cond.to_hdr(level) << " do\n"
        res << stmnt.to_hdr(level+1)
        res << " " * (level*3) << "end\n"
    end
    # Generate the no part if any.
    if self.no then
        res << " " * (level*3)
        res << "helse do\n" << self.no.to_hdr(level+1)
        res << " " * (level*3) << "end\n"
    end
    # Return the result.
    return res
end

#to_highObject

Creates a new high if statement.



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/HDLRuby/hruby_low2high.rb', line 272

def to_high
    # Is there a no?
    if self.no then
        # Yes, create a new if statement with it.
        res = HDLRuby::High::If.new(self.condition.to_high,
                                self.yes.to_high,self.no.to_high)
    else
        # No, create a new if statement without it.
        res = HDLRuby::High::If.new(self.condition.to_high,
                                self.yes.to_high)
    end
    # Add the noifs if any.
    self.each_noif do |cond,stmnt| 
        res.add_noif(cond.to_high,stmt.to_high)
    end
    return res
end

#to_seq!Object

Convert the block to seq.



75
76
77
78
79
80
81
# File 'lib/HDLRuby/hruby_low_without_parinseq.rb', line 75

def to_seq!
    self.to_seq!
    self.each_noif do |cond,blk|
        blk.to_seq!
    end
    self.no.to_seq! if self.no
end

#to_upper_space!Object

Moves the declarations to the upper namespace.



593
594
595
596
597
598
599
600
601
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 593

def to_upper_space!
    # Recurse on the sub blocks.
    # Yes.
    self.yes.to_upper_space!
    # Noifs.
    self.each_noif {|cond,stmnt| stmnt.to_upper_space! }
    # No if any.
    self.no.to_upper_space! if self.no
end

#to_verilog(spc = 3) ⇒ Object

Converts the system to Verilog code.

def to_verilog(mode = nil) Converts to Verilog code, checking adding 'spc' spaces at the begining of each line.



1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
# File 'lib/HDLRuby/hruby_verilog.rb', line 1700

def to_verilog(spc = 3)

    $blocking = false

    result = " " * spc  # Indented based on space_count.

    result << "if (#{self.condition.to_verilog}) "


    # Check if there is yes (if) and output yes or less.
    if self.respond_to? (:yes)
        result << self.yes.to_verilog(spc)
    end

    # If noif (else if) exists, it outputs it.
    # Since noif is directly under, respond_to is unnecessary.
    self.each_noif do |condition, block|
        result << "\n#{" "*spc}else if (#{condition.to_verilog}) "
        result << block.to_verilog(spc)
    end

    # Check if there is no (else) and output no or less.
    if self.no.respond_to?(:mode)
        result << "\n#{" " * spc}else "
        result << self.no.to_verilog(spc)
    end

    return result
end

#to_vhdl(vars, level = 0) ⇒ Object

Generates the text of the equivalent HDLRuby::High code. +vars+ is the list of the variables and +level+ is the hierachical level of the object.



961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
# File 'lib/HDLRuby/hruby_low2vhd.rb', line 961

def to_vhdl(vars,level = 0)
    # The result string.
    res = " " * (level*3)
    # Generate the test.
    res << "if (" << Low2VHDL.to_boolean(self.condition) << ") then\n"
    # Generate the yes part.
    res << self.yes.to_vhdl(vars,level+1)
    # Generate the alternate if parts.
    self.each_noif do |cond,stmnt|
        res << " " * (level*3)
        # res << "elsif (" << cond.to_vhdl(level) << ") then\n"
        res << "elsif (" << Low2VHDL.to_boolean(cond) << ") then\n"
        res << stmnt.to_vhdl(vars,level+1)
    end
    # Generate the no part if any.
    if self.no then
        res << " " * (level*3)
        res << "else\n" << self.no.to_vhdl(vars,level+1)
    end
    # Close the if.
    res << " " * (level*3)
    res << "end if;\n"
    # Return the result.
    return res
end

#to_viz_node(parent) ⇒ Object

Converts the if to a Viz flow node under +parent+.



4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
# File 'lib/HDLRuby/hruby_viz.rb', line 4635

def to_viz_node(parent)
  node = HDLRuby::Viz::Node.new(:if,parent)
  # Generate the condition.
  self.condition.to_viz_node(node)
  # And generate the sub statments.
  self.yes.to_viz_node(node)
  self.each_noif do |cond,stmnt|
    sub = HDLRuby::Viz::Node.new(:if,node)
    cond.to_viz_node(sub)
    stmnt.to_viz_node(sub)
  end
  self.no.to_viz_node(node) if self.no
  return node
end

#use_name?(*names) ⇒ Boolean

Tell if the statement includes a signal whose name is one of +names+. NOTE: for the if check only the condition.

Returns:

  • (Boolean)


3701
3702
3703
# File 'lib/HDLRuby/hruby_low.rb', line 3701

def use_name?(*names)
    return @condition.use_name?(*name)
end

#with_var(upper = nil) ⇒ Object

Converts to a variable-compatible if where +upper+ is the upper block if any.

NOTE: the result is a new if.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/HDLRuby/hruby_low_with_var.rb', line 283

def with_var(upper = nil)
    # Treat the sub nodes.
    # Condition.
    ncond = self.condition.clone
    # Yes.
    nyes =self.yes.with_var(upper)
    # Noifs.
    noifs = self.each_noif.map do |cond,stmnt|
        [cond.clone,stmnt.with_var(upper)]
    end
    # No.
    nno = self.no ? self.no.with_var(upper) : nil
    # Create the resulting If.
    res= If.new(ncond,nyes, nno)
    noifs.each do |cond,stmnt|
        res.add_noif(cond,stmnt)
    end
    return res
end