Class: HDLRuby::Low::Print

Inherits:
Statement
  • Object
show all
Defined in:
lib/HDLRuby/hruby_low.rb,
lib/HDLRuby/hruby_low2c.rb,
lib/HDLRuby/hruby_low2vhd.rb,
lib/HDLRuby/hruby_verilog.rb,
lib/HDLRuby/hruby_low2high.rb,
lib/HDLRuby/hruby_low_mutable.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_casts_without_expression.rb

Overview

Extends the Print class with functionality for extracting expressions from cast.

Direct Known Subclasses

High::Print

Constant Summary

Constants included from Low2Symbol

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

Instance Attribute Summary

Attributes included from Hparent

#parent

Instance Method Summary collapse

Methods inherited from Statement

#add_blocks_code, #add_make_block, #behavior, #block, #blocks2seq!, #break_types!, #delete_related!, #delete_unless!, #each_statement, #extract_declares!, #mix?, #par_in_seq2seq!, #parent_system, #replace_expressions!, #replace_names!, #scope, #to_ch, #to_hdr, #to_seq!, #to_upper_space!, #top_block, #top_scope, #use_name?, #with_boolean!

Methods included from Low2Symbol

#to_sym

Methods included from Hparent

#scope

Constructor Details

#initialize(*args) ⇒ Print

Creates a new statement for printing +args+.



3666
3667
3668
3669
3670
3671
3672
3673
# File 'lib/HDLRuby/hruby_low.rb', line 3666

def initialize(*args)
    super()
    # Process the arguments.
    @args = args.map do |arg|
        arg.parent = self 
        arg
    end
end

Instance Method Details

#boolean_in_assign2select!Object

Converts booleans in assignments to select operators.



71
72
73
74
75
# File 'lib/HDLRuby/hruby_low_bool2select.rb', line 71

def boolean_in_assign2select!
    # Apply on the arguments.
    self.map_args! { |arg| arg.boolean_in_assign2select }
    return self
end

#casts_without_expression!Object

Extracts the expressions from the casts.



72
73
74
75
76
# File 'lib/HDLRuby/hruby_low_casts_without_expression.rb', line 72

def casts_without_expression!
    # Apply on the arguments.
    self.map_args!(&:casts_without_expression)
    return self
end

#cloneObject

Clones the TimeWait (deeply)



3712
3713
3714
# File 'lib/HDLRuby/hruby_low.rb', line 3712

def clone
    return Print.new(*@args.map { |arg| arg.clone })
end

#delete_arg!(arg) ⇒ Object

Delete an arg.



645
646
647
648
649
650
651
652
653
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 645

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

#each_arg(&ruby_block) ⇒ Object

Iterates over each argument.

Returns an enumerator if no ruby block is given.



3687
3688
3689
3690
3691
3692
# File 'lib/HDLRuby/hruby_low.rb', line 3687

def each_arg(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_arg) unless ruby_block
    # A ruby block? First apply it to each argument.
    @args.each(&ruby_block)
end

#each_block(&ruby_block) ⇒ Object

Iterates over the sub blocks.



3736
3737
3738
3739
3740
3741
3742
3743
3744
# File 'lib/HDLRuby/hruby_low.rb', line 3736

def each_block(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_block) unless ruby_block
    # A ruby block?
    # Recurse on each argument.
    @args.each do |arg|
        arg.each_block(&ruby_block) if arg.respond_to?(:each_block)
    end
end

#each_block_deep(&ruby_block) ⇒ Object

Iterates over all the blocks contained in the current block.



3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
# File 'lib/HDLRuby/hruby_low.rb', line 3747

def each_block_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_block_deep) unless ruby_block
    # A ruby block?
    # Recurse on each argument.
    @args.each do |arg|
        if arg.respond_to?(:each_block_deep) then
            arg.each_block_deep(&ruby_block)
        end
    end
end

#each_deep(&ruby_block) ⇒ Object

Iterates over each object deeply.

Returns an enumerator if no ruby block is given.



3697
3698
3699
3700
3701
3702
3703
3704
# File 'lib/HDLRuby/hruby_low.rb', line 3697

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 arguments.
    self.each_arg(&ruby_block)
end

#each_node(&ruby_block) ⇒ Object

Iterates over the expression children if any.



3717
3718
3719
3720
3721
3722
3723
# File 'lib/HDLRuby/hruby_low.rb', line 3717

def each_node(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_node) unless ruby_block
    # A ruby block?
    # Apply it on each argument.
    @args.each(&ruby_block)
end

#each_node_deep(&ruby_block) ⇒ Object

Iterates over the nodes deeply if any.



3726
3727
3728
3729
3730
3731
3732
3733
# File 'lib/HDLRuby/hruby_low.rb', line 3726

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 apply it on each argument.
    @args.each(&ruby_block)
end

#each_statement_deep(&ruby_block) ⇒ Object

Iterates over all the statements contained in the current block.



3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
# File 'lib/HDLRuby/hruby_low.rb', line 3760

def each_statement_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_statement_deep) unless ruby_block
    # A ruby block?
    # Apply it on self.
    ruby_block.call(self)
    # Recurse on each argument.
    @args.each do |arg|
        if arg.respond_to?(:each_statement_deep) then
            arg.each_statement_deep(&ruby_block)
        end
    end
end

#eql?(obj) ⇒ Boolean

Comparison for hash: structural comparison.

Returns:

  • (Boolean)


3676
3677
3678
3679
3680
3681
3682
# File 'lib/HDLRuby/hruby_low.rb', line 3676

def eql?(obj)
    return false unless obj.is_a?(Print)
    return false if @args.each.zip(obj.each_arg).any? do |a0,a1|
        !a0.eql?(a1)
    end
    return true
end

#explicit_types!Object

Explicit the types conversions in the statement.



96
97
98
99
100
# File 'lib/HDLRuby/hruby_low_fix_types.rb', line 96

def explicit_types!
    # Recurse on the arguments.
    self.map_args!(&:explicit_types)
    return self
end

#extract_selects!Object

Extract the Select expressions.



170
171
172
173
174
175
176
# File 'lib/HDLRuby/hruby_low_without_select.rb', line 170

def extract_selects!
    selects = []
    self.map_args! do |arg|
        arg.extract_selects_to!(selects)
    end
    return selects
end

#hashObject

Hash function.



3707
3708
3709
# File 'lib/HDLRuby/hruby_low.rb', line 3707

def hash
    return @args.hash
end

#map_args!(&ruby_block) ⇒ Object Also known as: map_nodes!

Maps on the arguments.



634
635
636
637
638
639
640
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 634

def map_args!(&ruby_block)
    @args.map! do |arg|
        arg = ruby_block.call(arg)
        arg.parent = self unless arg.parent
        arg
    end
end

#replace_args!(node2rep) ⇒ Object

Replaces sub arguments 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.



661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 661

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

#to_c(level = 0) ⇒ Object

Generates the C text of the equivalent HDLRuby code. +level+ is the hierachical level of the object.



1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
# File 'lib/HDLRuby/hruby_low2c.rb', line 1132

def to_c(level = 0)
    # Save the state of the value pool.
    res = (" " * ((level)*3))
    res << "{\n"
    res << (" " * ((level+1)*3))
    res << "unsigned int pool_state = get_value_pos();\n"
    # Perform the copy and the touching only if the new content
    # is different.
    res << (" " * ((level+1)*3))
    # Is it a sequential execution model?
    seq = self.block.mode == :seq ? "_seq" : ""
    # Generate the print.
    self.each_arg do |arg|
        if (arg.is_a?(StringE)) then
            res << "printer.print_string(\"" + 
                Low2C.c_string(arg.content) + "\");\n"
        elsif (arg.is_a?(Expression)) then
            res << "printer.print_string_value(" + arg.to_c + ");\n"
        else
            res << "printer.print_string_name(" + arg.to_c + ");\n"
        end
    end
    # Restore the value pool state.
    res << (" " * ((level+1)*3))
    res << "set_value_pos(pool_state);\n"
    res << (" " * ((level)*3))
    res << "}\n"
    return res
end

#to_highObject

Creates a new high print statement.



309
310
311
312
# File 'lib/HDLRuby/hruby_low2high.rb', line 309

def to_high
    return HDLRuby::High::Print.new(
        *self.each_arg.map {|arg| arg.to_high })
end

#to_verilog(spc = 3) ⇒ Object

Converts the system to Verilog code.



118
119
120
121
122
123
# File 'lib/HDLRuby/hruby_verilog.rb', line 118

def to_verilog(spc = 3)
    code = "#{" " * spc}$write(#{self.each_arg.map do |arg|
    arg.to_verilog
    end.join(",") });"
    return code
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.



921
922
923
924
925
926
# File 'lib/HDLRuby/hruby_low2vhd.rb', line 921

def to_vhdl(vars,level = 0)
    # Generate a report statement.
    return " " * (level*3) + "report " + self.each_arg.map do |arg|
        arg.to_vhdl
    end.join(" & ") + ";\n"
end