Class: HDLRuby::Low::Chunk

Inherits:
Object
  • Object
show all
Includes:
Hparent
Defined in:
lib/HDLRuby/hruby_low.rb,
lib/HDLRuby/hruby_low2c.rb,
lib/HDLRuby/backend/hruby_c_allocator.rb

Overview

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

Direct Known Subclasses

High::Chunk

Instance Attribute Summary collapse

Attributes included from Hparent

#parent

Instance Method Summary collapse

Constructor Details

#initialize(name, *lumps) ⇒ Chunk

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



2404
2405
2406
2407
2408
2409
2410
# File 'lib/HDLRuby/hruby_low.rb', line 2404

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

Instance Attribute Details

#nameObject (readonly)

The name of the code chunk.



2401
2402
2403
# File 'lib/HDLRuby/hruby_low.rb', line 2401

def name
  @name
end

Instance Method Details

#add_lump(lump) ⇒ Object

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



2414
2415
2416
2417
2418
2419
2420
# File 'lib/HDLRuby/hruby_low.rb', line 2414

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) ⇒ Object

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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_lump(&ruby_block) ⇒ Object

Iterates over the code lumps.

Returns an enumerator if no ruby block is given.



2425
2426
2427
2428
2429
2430
# File 'lib/HDLRuby/hruby_low.rb', line 2425

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) ⇒ Object

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



916
917
918
919
920
921
922
923
924
925
926
# File 'lib/HDLRuby/hruby_low2c.rb', line 916

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