Class: HDLRuby::Low::Allocator

Inherits:
Object
  • Object
show all
Defined in:
lib/HDLRuby/backend/hruby_allocator.rb

Overview

An allocator.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range, word = 8) ⇒ Allocator

Creates a new allocator within +range+ memory space whose word size is +word+.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/HDLRuby/backend/hruby_allocator.rb', line 22

def initialize(range, word = 8)
    # Check and set the range.
    first = range.first.to_i
    last = range.last.to_i
    @range = first < last ? first..last : last..first
    # Check and set the word size.
    @word = word.to_i
    # Initialize the allocation counter.
    @head = first
    # Initialize the allocation table.
    @table = {}
end

Instance Attribute Details

#rangeObject (readonly)

The space range for the allocation.



15
16
17
# File 'lib/HDLRuby/backend/hruby_allocator.rb', line 15

def range
  @range
end

#wordObject (readonly)

The word size.



18
19
20
# File 'lib/HDLRuby/backend/hruby_allocator.rb', line 18

def word
  @word
end

Instance Method Details

#allocate(signal) ⇒ Object

Allocates space for +signal+. NOTE: if the signal is already allocated, returns the previous allocation result.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/HDLRuby/backend/hruby_allocator.rb', line 38

def allocate(signal)
    # Has the signal been already allocated?
    if @table.key?(signal) then
        # Yes return the allocation result.
        return @table[signal]
    end
    # Get the size to allocate in word.
    size = signal.type.width / @word
    size += 1 unless signal.type.width % word == 0
    # Is there any room left?
    if @head + size > @range.last then
        raise AnyError, "Address range overflow."
    end
    # Ok, performs the allocation.
    res = @head
    @head += size
    @table[signal] = res
    return res
end

#eachObject

Iterate over the allocated signals and their corresponding address.



64
65
66
# File 'lib/HDLRuby/backend/hruby_allocator.rb', line 64

def each
    @table.each
end

#get(signal) ⇒ Object

Get the address of +signal+ if allocated.



59
60
61
# File 'lib/HDLRuby/backend/hruby_allocator.rb', line 59

def get(signal)
    return @table[signal]
end