class HDLRuby::Low::Chunk

Extends the chunk class with support for self modification with allocation. NOTE: only work if the chunk is in C language.

Describes a non-HDLRuby code chunk.

Extend the Chunk cass with generation of text code.

Add the conversion to high.

Attributes

name[R]

The name of the code chunk.

Public Class Methods

new(name,*lumps) click to toggle source

Creates new code chunk name with made of lumps piece of text.

# File lib/HDLRuby/hruby_low.rb, line 2644
def initialize(name,*lumps)
    # Check and set the name.
    @name = name.to_sym
    # Set the content.
    @lumps = []
    lumps.each { |lump| self.add_lump(lump) }
end

Public Instance Methods

add_lump(lump) click to toggle source

Adds a lump of code, it is ment to become an expression or some text.

# File lib/HDLRuby/hruby_low.rb, line 2657
def add_lump(lump)
    # Set its parent if relevant.
    lump.parent = self if lump.respond_to?(:parent)
    # And add it
    @lumps << lump
    return lump
end
c_code_allocate!(allocator) click to toggle source

Allocates signal within C code using allocator and self-modify the code correspondingly. NOTE: non-C chunks are ignored.

# File lib/HDLRuby/backend/hruby_c_allocator.rb, line 45
def c_code_allocate!(allocator)
    # Checks the chunk is actually C.
    return self unless self.name == :c
    # Process each lump.
    @lumps.map! do |lump|
        lump_r = lump.resolve if lump.respond_to?(:resolve)
        if lump_r.is_a?(SignalI) then
            # The lump is a signal, performs the allocation and
            # change it to an address access.
            "*(0x#{allocator.allocate(lump_r).to_s(16)})"
        else
            lump
        end
    end
    self
end
each_deep(&ruby_block) click to toggle source

Iterates over each object deeply.

Returns an enumerator if no ruby block is given.

# File lib/HDLRuby/hruby_low.rb, line 2678
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)
end
each_lump(&ruby_block) click to toggle source

Iterates over the code lumps.

Returns an enumerator if no ruby block is given.

# File lib/HDLRuby/hruby_low.rb, line 2668
def each_lump(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_lump) unless ruby_block
    # A ruby block? Apply it on each lump.
    @lumps.each(&ruby_block)
end
to_c(level = 0) click to toggle source

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

# File lib/HDLRuby/hruby_low2c.rb, line 931
def to_c(level = 0)
    res = " " * level
    res << self.each_lump.map do |lump|
        if !lump.is_a?(String) then
            lump.respond_to?(:to_c) ? lump.to_c(level+1) : lump.to_s
        else
            lump
        end
    end.join
    return res
end
to_high() click to toggle source

Creates a new high code chunk.

# File lib/HDLRuby/hruby_low2high.rb, line 208
def to_high
    return HDLRuby::High::Chunk.new(self.name,
                        *self.each_lump { |lump| lump.to_high })
end