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

#hierarchy, #no_parent!, #scope

Constructor Details

#initialize(*args) ⇒ Print

Creates a new statement for printing +args+.



3753
3754
3755
3756
3757
3758
3759
3760
# File 'lib/HDLRuby/hruby_low.rb', line 3753

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)



3799
3800
3801
# File 'lib/HDLRuby/hruby_low.rb', line 3799

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

#delete_arg!(arg) ⇒ Object

Delete an arg.



657
658
659
660
661
662
663
664
665
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 657

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.



3774
3775
3776
3777
3778
3779
# File 'lib/HDLRuby/hruby_low.rb', line 3774

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.



3823
3824
3825
3826
3827
3828
3829
3830
3831
# File 'lib/HDLRuby/hruby_low.rb', line 3823

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.



3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
# File 'lib/HDLRuby/hruby_low.rb', line 3834

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.



3784
3785
3786
3787
3788
3789
3790
3791
# File 'lib/HDLRuby/hruby_low.rb', line 3784

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.



3804
3805
3806
3807
3808
3809
3810
# File 'lib/HDLRuby/hruby_low.rb', line 3804

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.



3813
3814
3815
3816
3817
3818
3819
3820
# File 'lib/HDLRuby/hruby_low.rb', line 3813

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.



3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
# File 'lib/HDLRuby/hruby_low.rb', line 3847

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)


3763
3764
3765
3766
3767
3768
3769
# File 'lib/HDLRuby/hruby_low.rb', line 3763

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.



3794
3795
3796
# File 'lib/HDLRuby/hruby_low.rb', line 3794

def hash
    return @args.hash
end

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

Maps on the arguments.



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

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.



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 673

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(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) # Save the state of the value pool. # res = (" " * ((level)*3)) res << (" " * ((level)*3)) res << " 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" res << "printer.print_string_value(" arg.to_c(res) res << ");\n" else # res << "printer.print_string_name(" + arg.to_c + ");\n" res << "printer.print_string_name(" arg.to_c(res) res << ");\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 Generates the C text of the equivalent HDLRuby code. +level+ is the hierachical level of the object. def to_c(level = 0)



1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
# File 'lib/HDLRuby/hruby_low2c.rb', line 1443

def to_c(res,level = 0)
    # Save the value pool state.
    res << (" " * (level*3)) << "PV;\n"
    # Generate the print for each argument.
    self.each_arg do |arg|
        if (arg.is_a?(StringE)) then
            res << (" " * (level*3))
            res << "printer.print_string(\"" + 
                Low2C.c_string(arg.content) + "\");\n"
        elsif (arg.is_a?(Expression)) then
            arg.to_c(res)
            res << (" " * (level*3))
            res << "printer.print_string_value(pop());\n"
        else
            arg.to_c(res)
            res << "printer.print_string_name(pop());\n"
        end
    end
    # Restore the value pool state.
    res << (" " * (level*3)) << "RV;\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 print to Verilog code.



175
176
177
178
179
180
# File 'lib/HDLRuby/hruby_verilog.rb', line 175

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